[英]convert array of object to associative array
I need to convert below array:- 我需要转换下面的数组:
Array
(
[0] => stdClass Object
(
[id] =>
[risk_reference] =>
[risk_version] =>
[bsi] => 10.00
)
)
to below array:- 到下面的数组:-
Array
(
[id] =>
[risk_reference] =>
[risk_version] =>
[bsi] => 10.00
)
I tried to do it by typecasting . 我试图通过类型转换来做到这一点。 But It didn't give me the output. 但是它没有给我输出。 I also checked this link 我也检查了这个链接
For Above $result = (array)($array[0]) works fine for me. 对于$ result =(array)($ array [0])以上对我来说很好。
But if I have the below then what will I do? 但是,如果我有以下内容,该怎么办?
Array
(
[0] => stdClass Object
(
[id] =>
[risk_reference] =>
[risk_version] =>
[bsi] => 10.00
)
[1] => stdClass Object
(
[id] =>
[risk_reference] =>
[risk_version] =>
[bsi] => 20.00
)
)
尝试这个
$array = (array)($array[0]);
try this 尝试这个
$yourArray = array();
$i=0;
foreach ($yourObject as $key => $value) {
$yourArray[$i]['id'] = $value->id;
$yourArray[$i]['risk_reference'] = $value->risk_reference;
$yourArray[$i]['risk_version'] = $value->risk_version;
$yourArray[$i]['bsi'] = $value->bsi;
$i+=1;
}
print_r($yourArray);
http://php.net/get_object_vars http://php.net/get_object_vars
Gets the accessible non-static properties of the given object according to scope. 根据范围获取给定对象的可访问非静态属性。
Returns an associative array of defined object accessible non-static properties for the specified object in scope. 返回范围内指定对象的已定义对象可访问非静态属性的关联数组。 If a property has not been assigned a value, it will be returned with a NULL value. 如果尚未为属性分配值,则将返回NULL值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.