Home > Article > Backend Development > php object to array
php object to array method: 1. Use the statement "$data=(array)$Object;" to convert object to array; 2. Convert object to array through the custom "object_array" method.
Recommended: "PHP Video Tutorial"
Conversion of PHP Arrays and Objects
1 Convert array to Object
$array=array('a' =>'哈哈','b' =>'bnbbbb','西' =>'嘻嘻嘻'); $vl=json_encode($array); $ob=json_decode($vl); echo "<pre class="brush:php;toolbar:false">"; print_r($ob);
2 Convert Object to array
Add (array) before the Object variable
Example 1:
$data=(array)$Object; var_dump($data);
Example 2:
//或者调用这个函数,将其幻化为数组,然后取出对应值 function object_array($array) { if(is_object($array)) { $array = (array)$array; } if(is_array($array)) { foreach($array as $key=>$value) { $array[$key] = object_array($value); } } return $array; }
The above is the detailed content of php object to array. For more information, please follow other related articles on the PHP Chinese website!