以特定格式转换和数组到对象

时间:2013-08-21 23:11:41

标签: php arrays sorting object data-structures

转换此数组:

Array
    (
        [0] => Array
            (
                [TEST] => Array
                    (
                        [name] => John Snow
                        [id] => 2
                    )
                [HELLO] => Array
                    (
                        [stuff] => what 
                        [more] => 0
                    )
            )

        [1] => Array
            (
                [TEST] => Array
                    (
                        [name] => Arya Stark
                        [id] => 3
                    )
                [HELLO] => Array
                    (
                        [stuff] => dsfsdf dfsd 
                        [more] => 3
                    )
            )

        [2] => Array
            (
                [TEST] => Array
                    (
                        [name] => Tyrion Lannister 
                        [id] => 7
                    )
                [HELLO] => Array
                    (
                        [stuff] => test 
                        [more] => 2
                    )
            )
    )

要:

stdClass Object
        (
            [1] => stdClass Object
                (
                    [TEST] => stdClass Object
                        (
                            [name] => John Snow
                            [id] => 2
                        )

                    [HELLO] => stdClass Object
                        (
                            [stuff] => what 
                            [more] => 0
                        )

                )
            [2] => stdClass Object
                (
                    [TEST] => stdClass Object
                        (
                            [name] => Arya Stark
                            [id] => 3
                        )
                    [HELLO] => stdClass Object
                        (
                            [stuff] => dsfsdf dfsd 
                            [more] => 3
                        )
                )

            [3] => stdClass Object
                (
                    [TEST] => stdClass Object
                        (
                            [name] => Tyrion Lannister 
                            [id] => 7
                        )
                    [HELLO] => stdClass Object
                        (
                            [stuff] => test 
                            [more] => 2
                        )
                )


        )

我们每个级别可以有多个数组。这只是虚拟数据,名称永远不是TEST或HELLO,可以是任何东西。如果你们需要更多信息,请告诉我们!

这是我到目前为止所拥有的:

function (&$data, $index) {
        $object = new stdClass();
        $output = array();
        foreach ($data as $key => $value) {
             $object->$key->$index = $value[$index];
         }
    return $object;
 };

2 个答案:

答案 0 :(得分:0)

你应该能够做到这一点:

$objects = (object)json_decode(json_encode($array));

答案 1 :(得分:0)

<?php

function convert($data)
{
    if (is_array($data))
    {
        $obj = new stdClass();
        foreach ($data as $k => $v)
        {
            if (is_array($v))
            {
                $obj->$k = convert($v);
            }
            else
            {
                $obj->$k = $v;
            }
        }
        return $obj;
    }
}

$data = array(
    array(
        'Test' => array(
            'Name' => 'John Snow',
            'id' => 2,
        ),
        'Hello' => array(
            'Name' => 'what',
            'id' => 3,
        ),
    ),
);
$a = convert($data);
print_r($a);