-1

この配列を変換します。

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;
 };
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 に答える