php 如何将多维数组转换为php中的对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9169892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to convert multidimensional array to object in php?
提问by Patrioticcow
i have a multidimensional array:
我有一个多维数组:
$image_path = array('sm'=>$sm,'lg'=>$lg,'secondary'=>$sec_image);
witch looks like this:
女巫长这样:
[_media_path:protected] => Array
(
[main_thumb] => http://example.com/e4150.jpg
[main_large] => http://example.com/e4150.jpg
[secondary] => Array
(
[0] => http://example.com/e4150.jpg
[1] => http://example.com/e4150.jpg
[2] => http://example.com/e9243.jpg
[3] => http://example.com/e9244.jpg
)
)
and i would like to convert it into an object and retain the key names.
我想将它转换成一个对象并保留键名。
Any ideas?
有任何想法吗?
Thanks
谢谢
edit: $obj = (object)$image_path;
doesn't seem to work. i need a different way of looping through the array and creating a object
编辑:$obj = (object)$image_path;
似乎不起作用。我需要一种不同的方式来循环遍历数组并创建一个对象
回答by Charlie Schliesser
A quick way to do this is:
一个快速的方法是:
$obj = json_decode(json_encode($array));
Explanation
解释
json_encode($array)
will convert the entire multi-dimensional array to a JSON string. (php.net/json_encode)
json_encode($array)
将整个多维数组转换为 JSON 字符串。( php.net/json_encode)
json_decode($string)
will convert the JSON string to a stdClass
object. If you pass in TRUE
as a second argument to json_decode
, you'll get an associative array back. (php.net/json_decode)
json_decode($string)
将 JSON 字符串转换为stdClass
对象。如果你TRUE
作为第二个参数传入json_decode
,你会得到一个关联数组。( php.net/json_decode)
I don't think the performance here vs recursively going through the array and converting everything is very noticeable, although I'd like to see some benchmarks of this. It works, and it's not going to go away.
我不认为这里的性能与递归遍历数组并转换所有内容的性能非常显着,尽管我希望看到一些基准测试。它有效,而且不会消失。
回答by Wayne Weibel
The best way would be to manage your data structure as an object from the start if you have the ability:
如果您有能力,最好的方法是从一开始就将您的数据结构作为一个对象进行管理:
$a = (object) array( ... ); $a->prop = $value; //and so on
But the quickest way would be the approach supplied by @CharlieS, using json_decode(json_encode($a))
.
但最快的方法是@CharlieS 提供的方法,使用json_decode(json_encode($a))
.
You could also run the array through a recursive function to accomplish the same. I have not benchmarked this against the json approach but:
您还可以通过递归函数运行数组来完成相同的操作。我没有针对 json 方法对此进行基准测试,但是:
function convert_array_to_obj_recursive($a) {
if (is_array($a) ) {
foreach($a as $k => $v) {
if (is_integer($k)) {
// only need this if you want to keep the array indexes separate
// from the object notation: eg. $o->{1}
$a['index'][$k] = convert_array_to_obj_recursive($v);
}
else {
$a[$k] = convert_array_to_obj_recursive($v);
}
}
return (object) $a;
}
// else maintain the type of $a
return $a;
}
Hope that helps.
希望有帮助。
EDIT: json_encode + json_decode will create an object as desired. But, if the array was numerical or mixed indexes (eg. array('a', 'b', 'foo'=>'bar')
), you will not be able to reference the numerical indexes with object notation (eg. $o->1 or $o[1]). the above function places all the numerical indexes into the 'index' property, which is itself a numerical array. so, you would then be able to do $o->index[1]
. This keeps the distinction of a converted array from a created object and leaves the option to merge objects that may have numerical properties.
编辑: json_encode + json_decode 将根据需要创建一个对象。但是,如果数组是数字索引或混合索引(例如array('a', 'b', 'foo'=>'bar')
),您将无法使用对象表示法(例如 $o->1 或 $o[1])引用数字索引。上面的函数将所有数字索引放入 'index' 属性,它本身就是一个数字数组。所以,你就可以做到$o->index[1]
。这保留了转换后的数组与创建的对象的区别,并保留了合并可能具有数字属性的对象的选项。