欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP数组、对象的转换

程序员文章站 2022-05-31 17:54:20
...
PHP多维数组、对象的转换Array2object and Object2array
August 4, 2009 – 9:51 pm

function object2array($object) {
if (is_object($object) || is_array($object)) {
foreach ($object as $key => $value) {
print “$key\r\n”;
$array[$key] = $this->object2array($value);
}
}else {
$array = $object;
}
return $array;
}

function array2object($arrGiven){
//create empty class
$objResult=new stdClass();

foreach ($arrLinklist as $key => $value){
//recursive call for multidimensional arrays
if(is_array($value)) $value=array2object($value);

$objResult->{$key}=$value;
}
return $objResult;
}