首页 > 在php中读取stdclass的问题?

在php中读取stdclass的问题?

例如:

array(3) { [0]=> object(stdClass)#6 (2) 
               { ["id"]=> string(1) "1" ["topic_name"]=> string(5) "janus" }
          [1]=> object(stdClass)#7 (2) 
               { ["id"]=> string(2) "10" ["topic_name"]=> string(8) "emulsion" } 
          [2]=> object(stdClass)#33 (2) 
               { ["id"]=> string(2) "14" ["topic_name"]=> string(8) "particle" } }

如果转换成数组?


谢邀。
直接通过常用的->获取类属性的方式进行操作,如果需要获取当中有什么元素的化可以使用反射实现

$reflection = new ReflectionObject($stdClass);
$properties = $target->getProperties();

ReflectionObject::getProperties


用foreach就可以


写个函数,用上array_map


不太严格要求性能且编码是 utf-8 的情况下就:

$new_array = json_decode(json_encode($array), true);

如果严格要求的话,根据情况循环处理吧

foreach ($array as $key => $value) {
    $array[$key] = (array)$value;
}
【热门文章】
【热门文章】