php 如何使用 stdClass() 将数组转换为对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19272011/
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 an array into an object using stdClass()
提问by Alexandrw
I have made the following array:
我制作了以下数组:
$clasa = array(
'e1' => array('nume' => 'Nitu', 'prenume' => 'Andrei', 'sex' => 'm', 'varsta' => 23),
'e2' => array('nume' => 'Nae', 'prenume' => 'Ionel', 'sex' => 'm', 'varsta' => 27),
'e3' => array('nume' => 'Noman', 'prenume' => 'Alice', 'sex' => 'f', 'varsta' => 22),
'e4' => array('nume' => 'Geangos', 'prenume' => 'Bogdan', 'sex' => 'm', 'varsta' => 23),
'e5' => array('nume' => 'Vasile', 'prenume' => 'Mihai', 'sex' => 'm', 'varsta' => 25)
);
I would like to know how to convert this array into an object using stdClass(), I'm a PHP beginner, a simple example would be very helpful, I've tried searching for similar questions, but the answers are complicated and go beyond my understanding of basic classes and objects.
我想知道如何使用 stdClass() 将这个数组转换为对象,我是 PHP 初学者,一个简单的例子会很有帮助,我已经尝试搜索类似的问题,但答案很复杂,而且超出了我对基本类和对象的理解。
回答by Ekramul Hoque
You just add this code
您只需添加此代码
$clasa = (object) array(
'e1' => array('nume' => 'Nitu', 'prenume' => 'Andrei', 'sex' => 'm', 'varsta' => 23),
'e2' => array('nume' => 'Nae', 'prenume' => 'Ionel', 'sex' => 'm', 'varsta' => 27),
'e3' => array('nume' => 'Noman', 'prenume' => 'Alice', 'sex' => 'f', 'varsta' => 22),
'e4' => array('nume' => 'Geangos', 'prenume' => 'Bogdan', 'sex' => 'm', 'varsta' => 23),
'e5' => array('nume' => 'Vasile', 'prenume' => 'Mihai', 'sex' => 'm', 'varsta' => 25)
);
If you want to see is this stdClass object just call this
如果你想看看是这个 stdClass 对象就调用这个
print_r($clasa);
If you want to convert an array to object code will be
如果要将数组转换为目标代码将是
$arr = array('a'=>'apple','b'=>'ball');
$arr = (object) $arr;
You don't need to use stdClass. It will automatically converted to stdClass
您不需要使用 stdClass。它会自动转换为 stdClass
回答by h2ooooooo
The quick and dirty way is using json_encode
and json_decode
which will turn the entire array (including sub elements) into an object.
快速而肮脏的方法是使用json_encode
andjson_decode
它将整个数组(包括子元素)变成一个对象。
$clasa = json_decode(json_encode($clasa)); //Turn it into an object
The same can be used to convert an object into an array. Simply add , true
to json_decode
to return an associated array:
同样可以用于将对象转换为数组。只需添加, true
tojson_decode
即可返回关联的数组:
$clasa = json_decode(json_encode($clasa), true); //Turn it into an array
An alternate way (without being dirty) is simply a recursive function:
另一种方法(不脏)只是一个递归函数:
function convertToObject($array) {
$object = new stdClass();
foreach ($array as $key => $value) {
if (is_array($value)) {
$value = convertToObject($value);
}
$object->$key = $value;
}
return $object;
}
or in full code:
或完整代码:
<?php
function convertToObject($array) {
$object = new stdClass();
foreach ($array as $key => $value) {
if (is_array($value)) {
$value = convertToObject($value);
}
$object->$key = $value;
}
return $object;
}
$clasa = array(
'e1' => array('nume' => 'Nitu', 'prenume' => 'Andrei', 'sex' => 'm', 'varsta' => 23),
'e2' => array('nume' => 'Nae', 'prenume' => 'Ionel', 'sex' => 'm', 'varsta' => 27),
'e3' => array('nume' => 'Noman', 'prenume' => 'Alice', 'sex' => 'f', 'varsta' => 22),
'e4' => array('nume' => 'Geangos', 'prenume' => 'Bogdan', 'sex' => 'm', 'varsta' => 23),
'e5' => array('nume' => 'Vasile', 'prenume' => 'Mihai', 'sex' => 'm', 'varsta' => 25)
);
$obj = convertToObject($clasa);
print_r($obj);
?>
which outputs (note that there's no arrays - only stdClass
's):
哪个输出(请注意,没有数组 - 只有stdClass
's):
stdClass Object
(
[e1] => stdClass Object
(
[nume] => Nitu
[prenume] => Andrei
[sex] => m
[varsta] => 23
)
[e2] => stdClass Object
(
[nume] => Nae
[prenume] => Ionel
[sex] => m
[varsta] => 27
)
[e3] => stdClass Object
(
[nume] => Noman
[prenume] => Alice
[sex] => f
[varsta] => 22
)
[e4] => stdClass Object
(
[nume] => Geangos
[prenume] => Bogdan
[sex] => m
[varsta] => 23
)
[e5] => stdClass Object
(
[nume] => Vasile
[prenume] => Mihai
[sex] => m
[varsta] => 25
)
)
So you'd refer to it by $obj->e5->nume
.
所以你可以通过$obj->e5->nume
.
回答by darleys
If you want to recursively convert the entire array into an Object type (stdClass) then , below is the best method and it's not time-consuming or memory deficient especially when you want to do a recursive (multi-level) conversion compared to writing your own function.
如果您想递归地将整个数组转换为对象类型 (stdClass) 那么,下面是最好的方法,它不耗时或内存不足,尤其是当您想要进行递归(多级)转换时,与编写您的自己的功能。
$array_object = json_decode(json_encode($array));
回答by SESN
One of the easiest solution is
最简单的解决方案之一是
$objectData = (object) $arrayData
回答by Vikas_web
To convert array to object using stdClass just add (object)
to array u declare.
要使用 stdClass 将数组转换为对象,只需添加(object)
到您声明的数组中即可。
EX:
前任:
echo $array['value'];
echo $object->value;
to convert object to array
将对象转换为数组
$obj = (object)$array;
to convert array to object
将数组转换为对象
$arr = (array)$object
with these methods you can swap between array and object very easily.
使用这些方法,您可以非常轻松地在数组和对象之间进行交换。
Another method is to use json
另一种方法是使用json
$object = json_decode(json_encode($array), FALSE);
But this is a much more memory intensive way to do and is not supported by versions of PHP <= 5.1
但这是一种占用更多内存的方法,并且 PHP <= 5.1 版本不支持
回答by Beaudinn Greve
Array to stdClasscan be done in php this way. (stdClass is already PHP's generic empty class)
数组到stdClass可以通过这种方式在 php 中完成。(stdClass 已经是 PHP 的通用空类)
$a = stdClass:: __set_state(array());
Actually calling stdClass::__set_state() in PHP 5 will produce a fatal error. thanks @Ozzy for pointing out
实际上在 PHP 5 中调用 stdClass::__set_state() 会产生致命错误。感谢@Ozzy 指出
This is an example of how you can use __set_state() with a stdClass object in PHP5
这是一个示例,说明如何在 PHP5 中将 __set_state() 与 stdClass 对象一起使用
class stdClassHelper{
public static function __set_state(array $array){
$stdClass = new stdClass();
foreach ($array as $key => $value){
$stdClass->$key = $value;
}
return $stdClass;
}
}
$newstd = stdClassHelper::__set_state(array());
Or a nicer way.
或者更好的方式。
$a = (object) array();
回答by yussan
or you can use this thing
或者你可以用这个东西
$arr = [1,2,3];
$obj = json_decode(json_encode($arr));
print_r($obj);
回答by Shakti Patel
use this Tutorial
使用本 教程
<?php
function objectToArray($d) {
if (is_object($d)) {
// Gets the properties of the given object
// with get_object_vars function
$d = get_object_vars($d);
}
if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return array_map(__FUNCTION__, $d);
}
else {
// Return array
return $d;
}
}
function arrayToObject($d) {
if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return (object) array_map(__FUNCTION__, $d);
}
else {
// Return object
return $d;
}
}
// Create new stdClass Object
$init = new stdClass;
// Add some test data
$init->foo = "Test data";
$init->bar = new stdClass;
$init->bar->baaz = "Testing";
$init->bar->fooz = new stdClass;
$init->bar->fooz->baz = "Testing again";
$init->foox = "Just test";
// Convert array to object and then object back to array
$array = objectToArray($init);
$object = arrayToObject($array);
// Print objects and array
print_r($init);
echo "\n";
print_r($array);
echo "\n";
print_r($object);
//OUTPUT
stdClass Object
(
[foo] => Test data
[bar] => stdClass Object
(
[baaz] => Testing
[fooz] => stdClass Object
(
[baz] => Testing again
)
)
[foox] => Just test
)
Array
(
[foo] => Test data
[bar] => Array
(
[baaz] => Testing
[fooz] => Array
(
[baz] => Testing again
)
)
[foox] => Just test
)
stdClass Object
(
[foo] => Test data
[bar] => stdClass Object
(
[baaz] => Testing
[fooz] => stdClass Object
(
[baz] => Testing again
)
)
[foox] => Just test
)