简体   繁体   English

动态创建要编码为JSON的多维数组

[英]Dynamically creating multidimensional arrays to be encoded into JSON

I have a section of an array defined: 我定义了数组的一部分:

$arrData['categories'] = array();
array_push($arrData['categories'],array("category" => array(array("label"=>"Beef"),array("label"=>"Chicken"))));
array_push($arrDAta['categories'][0]['category'],array("label"=>"pork"));

The arrays are nested so that it encodes into JSON properly with all the {[{[{[]}]}]} formatted properly. 数组是嵌套的,因此可以正确格式化所有{[{[[[[[[]]}}]]}}的格式,并将其正确编码为JSON。

However, I need to create these types of meats dynamically, not statically. 但是,我需要动态地而不是静态地创建这些类型的肉。 When I try to add onto the array in the third line of code to simulate dynamic creation, it throws an error: 当我尝试在第三行代码中添加到数组以模拟动态创建时,它会引发错误:

Warning: array_push() expects parameter 1 to be an array, null given 警告:array_push()期望参数1为数组,给定null

Well, $arrData['categories'][0]['category] IS an array, so why does it shoot me down? 好吧, $arrData['categories'][0]['category]是一个数组,为什么它使我失望?

To show myself that I'm not crazy, I var_dump $arrData['categories'][0]['category'] and get an array of size 1: 为了表明自己不发疯,我使用var_dump $arrData['categories'][0]['category']并获取大小为1的数组:

array (size=1)
'category' =>
 array (size=2)
   0 => 
     array (size=1)
       'label' => string 'Beef' (length=4)
   1 => 
     array (size=1)
       'label' => string 'Chicken' (length=7)

Its the capitalization. 其大写。 In the third line the variable $arrDAta is capitalized differently than the others ($arrData) 在第三行中,变量$ arrDAta的大写形式与其他变量($ arrData)不同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM