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

php object to array

藏色散人
Release: 2023-03-04 08:04:02
Original
3428 people have browsed it

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);
Copy after login

2 Convert Object to array

Add (array) before the Object variable

Example 1:

$data=(array)$Object;
var_dump($data);
Copy after login

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;
}
Copy after login

The above is the detailed content of php object to array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template