-1

Convert this array:

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

To:

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


        )

We can have more than one array within each level. This is just dummy data and the names are never TEST or HELLO, and can be anything. Let me know if you guys need more info!

Here's what I have so far:

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

2 回答 2

0

您应该可以这样做:

$objects = (object)json_decode(json_encode($array));
于 2013-08-21T23:30:07.110 回答
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);
于 2013-08-21T23:30:32.503 回答