如何使用 stdClass()将数组转换为对象

我制作了以下数组:

$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)
);

我想知道如何使用 stdClass ()将这个数组转换成一个对象,我是一个 PHP 初学者,一个简单的例子会非常有帮助,我已经尝试搜索类似的问题,但答案是复杂的,超出了我对基本类和对象的理解。

217327 次浏览

你只需要添加这个代码

$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

print_r($clasa);

如果要将数组转换为目标代码,则

$arr = array('a'=>'apple','b'=>'ball');
$arr = (object) $arr;

您不需要使用 stdClass,它将自动转换为 stdClass

最快捷的方法是使用 json_encodejson_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:

$clasa = json_decode(json_encode($clasa), true); //Turn it into an array

另一种方法(不是肮脏的方法)就是简单的递归函数:

function convertToObject($array) {
$object = new stdClass();
foreach ($array as $key => $value) {
if (is_array($value)) {
$value = convertToObject($value);
}
$object->$key = $value;
}
return $object;
}

或完整代码:

<?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);
?>

输出(注意,没有数组,只有 stdClass) :

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
)


)

所以你可以用 $obj->e5->nume引用它。

DEMO

用这个 教程

<?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
)

要使用 stdClass 将数组转换为对象,只需将 (object)添加到数组 u 声明。

例如:

echo $array['value'];
echo $object->value;

将对象转换为数组

$obj = (object)$array;

to convert array to object

$arr = (array)$object

使用这些方法,您可以很容易地在数组和对象之间进行交换。


Another method is to use json

$object = json_decode(json_encode($array), FALSE);

但这是一种内存密集得多的方式,PHP < = 5.1版本不支持这种方式

数组到 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

这是如何在 PHP5中对 stdClass 对象使用 _ _ set _ state ()的示例

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());

或者更好的方式。

$a = (object) array();

如果你想要递归地将整个数组转换成 Object 类型(stdClass) ,那么下面是最好的方法,它不会耗费时间或者内存不足,特别是当你想要做一个递归(多级)转换时,相比于编写你自己的函数。

$array_object = json_decode(json_encode($array));

最简单的解决办法之一就是

$objectData = (object) $arrayData

或者你可以用这个

$arr = [1,2,3];
$obj = json_decode(json_encode($arr));
print_r($obj);