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

时间:2022-10-23 07:54:45

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初学者,一个简单的示例将非常有用,我尝试搜索类似的问题,但是答案很复杂,超出了我对基本类和对象的理解。

8 个解决方案

#1


164  

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

#2


38  

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和json_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:

同样,也可以将对象转换为数组。简单地说,json_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):

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->。

DEMO

演示

#3


11  

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

#4


5  

To convert array to object using stdClass just add (object) to array u declare.

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

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


5  

One of the easiest solution is

最简单的解决方法之一是

$objectData = (object) $arrayData

#6


4  

Array to stdClass can be done in php this way. (stdClass is already PHP's generic empty class)

以这种方式,可以用php来完成对stdClass的数组。(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中使用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());

Or a nicer way.

或更好的方式。

$a = (object) array();

#7


3  

or you can use this thing

或者你可以用这个

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

#8


1  

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
)

#1


164  

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

#2


38  

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和json_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:

同样,也可以将对象转换为数组。简单地说,json_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):

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->。

DEMO

演示

#3


11  

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

#4


5  

To convert array to object using stdClass just add (object) to array u declare.

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

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


5  

One of the easiest solution is

最简单的解决方法之一是

$objectData = (object) $arrayData

#6


4  

Array to stdClass can be done in php this way. (stdClass is already PHP's generic empty class)

以这种方式,可以用php来完成对stdClass的数组。(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中使用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());

Or a nicer way.

或更好的方式。

$a = (object) array();

#7


3  

or you can use this thing

或者你可以用这个

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

#8


1  

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
)