如何在对象数组中设置索引?

时间:2018-07-02 10:03:25

标签: php arrays arraylist multidimensional-array

我有一个数组wk,为此输出是

[wk] => Array
        (
            [1] => Array
                (
                    [2] => stdClass Object
                        (
                            [qty] => 2
                            [id] => 2
                            [name] => Single 1 Tag Innenraum 2
                        )

                    [3] => stdClass Object
                        (
                            [qty] => 1
                            [id] => 3
                            [name] => test1
                        )

                )

        )

但是我想在对象中添加新索引...如何添加它?任何想法...我试图做最后两天,但没有任何结果

我想要这样的输出:

[wk] => Array
        (
            [1] => Array
                (
                    [2] => stdClass Object
                        (
                            [qty] => 2
                            [id] => 2
                            [name] => Single 1 Tag Innenraum 2
                            [1] => Array(
                                    [name] => jiya
                                    [sirname] => rathod
                                        )
                            [2] => Array(
                                    [name] => pinal
                                    [sirname] => rathod
                                        )

                        )

                    [3] => stdClass Object
                        (
                            [qty] => 1
                            [id] => 3
                            [name] => test1
                        )

                )
        )

我使用了array_walk和array_push,array_merge函数...

上述所有功能我都收到此错误

致命错误:未被捕获的错误:无法将stdClass类型的对象用作数组,

2 个答案:

答案 0 :(得分:1)

您需要了解stdClass Object使用类变量名。因此,将不可能使用数字1作为变量名。取而代之的是:

...
[2] => stdClass Object
 (
  [qty] => 2
  [id] => 2
  [name] => Single 1 Tag Innenraum 2
  [one] => Array(
        [name] => jiya
        [sirname] => rathod
      )
  [two] => Array(
        [name] => jiya
        [sirname] => rathod
      )
...

用有效的变量名替换数字,例如上面的one

要添加新元素,您可以调用:

wk[1][2]->one = Array(
        [name] => jiya
        [sirname] => rathod
      );

可通过wk[1][2]->one轻松访问哪个

答案 1 :(得分:-1)

您可以通过两种方式解决此问题:

1)将对象转换为数组

2)将新数组分配给对象,将其命名为“ persons”或类似名称:

$wk[1][2]->persons = array(); // Or via foreach, etc.

,然后使用array_push插入该数组:

array_push($wk[1][2]->persons, '...');

[wk] => Array
    (
        [1] => Array
            (
                [2] => stdClass Object
                    (
                        [qty] => 2
                        [id] => 2
                        [name] => Single 1 Tag Innenraum 2
                        [persons] => Array(
                            [1] => Array(
                                [name] => jiya
                                [sirname] => rathod
                                    ),
                            [2] => Array(
                                [name] => pinal
                                [sirname] => rathod
                                    ),
                         )

                    )

                [3] => stdClass Object
                    (
                        [qty] => 1
                        [id] => 3
                        [name] => test1
                    )

            )
    )