问题
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.
回答1:
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
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
回答2:
The quick and dirty way is using json_encode and json_decode which will turn the entire array (including sub elements) into an object.
$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
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 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.
DEMO
回答3:
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.
$array_object = json_decode(json_encode($array));
回答4:
One of the easiest solution is
$objectData = (object) $arrayData
回答5:
To convert array to object using stdClass just add (object) to array u declare.
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
$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
回答6:
Array to stdClass can be done in php this way. (stdClass is already PHP's generic empty class)
$a = stdClass:: __set_state(array());
Actually calling stdClass::__set_state() in PHP 5 will produce a fatal error. thanks @Ozzy for pointing out
This is an example of how you can use __set_state() with a stdClass object in PHP5
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();
回答7:
or you can use this thing
$arr = [1,2,3];
$obj = json_decode(json_encode($arr));
print_r($obj);
回答8:
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
)
来源:https://stackoverflow.com/questions/19272011/how-to-convert-an-array-into-an-object-using-stdclass