Problem
The following array was created by me:
$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’m a PHP beginner and would appreciate a simple example of how to transform this array into an object using stdClass(). I’ve tried looking for similar topics, but the solutions are convoluted and go beyond my comprehension of basic classes and objects.
Asked by Alexandrw
Solution #1
You only need to paste this code into your browser.
$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)
);
Simply call this to see if this stdClass object exists.
print_r($clasa);
If you wish to convert an array to object code, you’ll need to use the following code.
$arr = array('a'=>'apple','b'=>'ball');
$arr = (object) $arr;
There’s no need to use stdClass. It will be transformed to stdClass automatically.
Answered by Ekramul Hoque
Solution #2
Using json encode and json decode, which will convert the full array (including sub items) into an object, is a quick and dirty solution.
$clasa = json_decode(json_encode($clasa)); //Turn it into an object
An object can be converted to an array in the same way. To get an associated array, simply add, true to json decode:
$clasa = json_decode(json_encode($clasa), true); //Turn it into an array
A recursive function is a simple alternative (that isn’t dirty):
function convertToObject($array) {
$object = new stdClass();
foreach ($array as $key => $value) {
if (is_array($value)) {
$value = convertToObject($value);
}
$object->$key = $value;
}
return $object;
}
Alternatively, 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 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
)
)
As a result, you’d use $obj->e5->nume to refer to it.
DEMO
Answered by h2ooooooo
Solution #3
If you wish to recursively transform the entire array into an Object type (stdClass), then the approach described below is the best option. It is not time-consuming or memory-intensive, especially when compared to implementing your own function.
$array_object = json_decode(json_encode($array));
Answered by darleys
Solution #4
One of the most straightforward options is
$objectData = (object) $arrayData
Answered by SESN
Solution #5
Simply add (object) to the array you declare to convert it to an object using stdClass.
EX:
echo $array['value'];
echo $object->value;
to make an object into an array
$obj = (object)$array;
to change an array into an object
$arr = (array)$object
You can easily switch between array and object using these methods.
Another option is to make use of json.
$object = json_decode(json_encode($array), FALSE);
However, this is a far more memory-intensive method that is not supported by PHP versions prior to 5.1.
Answered by Vikas_web
Post is based on https://stackoverflow.com/questions/19272011/how-to-convert-an-array-into-an-object-using-stdclass