Home >Backend Development >PHP Problem >php object to array

php object to array

藏色散人
藏色散人Original
2020-08-10 10:32:563377browse

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.

php object to array

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn