PHP objects and arrays to convert each other, PHP arrays to convert each other
The examples in this article describe how PHP objects and arrays are converted to and from each other. Share to everyone for your reference. The specific analysis is as follows:
This defines 2 php anonymous objects and arrays of functions that are converted to each other, with the following code:
function Array2object ($array) { if (Is_array ($array)) { $obj = new StdClass (); foreach ($array as $key = + $val) { $obj $key = $val; } } else {$obj = $array;} return $obj;} function Object2array ($object) { if (Is_object ($object)) { foreach ($object as $key = = $value) { $array [ $key] = $value; } } else { $array = $object; } return $array;}
The usage examples are as follows:
$array = Array (' foo ' = ' bar ', ' one ' = ' + ', ' three ' = ' four '), $obj = Array2object ($array);p rint $obj->one; Output ' s "of" $arr = Object2array ($obj);p rint $arr [' foo ']; Output ' s bar
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/998574.html www.bkjia.com true http://www.bkjia.com/PHPjc/998574.html techarticle PHP object and array conversion methods, PHP arrays to each other This article describes the PHP objects and arrays to convert each other method. Share to everyone for your reference. The specific analysis is as follows: ...