导航返回的对象


Navigating a returned object

在PHP中,我有一个多维对象,它是通过循环遍历ID 列表创建的

 $summary = array();
 foreach ( $request->id as $id ) {
 ...
 $summary[] = $summary_data;
 }

然后它被传递给我的javascript。

 return json_encode(array('summary' => $summary));

不确定如何正确导航返回的对象。我是否必须使用id的原始列表,并将其用作此对象的索引?或者有更好的方法来跟踪这一点吗?

最终结果,我想要一个选择框,这样当选择一个新项目时,它的数据就会显示出来。

一个通用的JSON对象看起来是这样的(试图放置所有可能的情况):

{
    "key1":"value1", 
    "subObject":{
        "subKey1":"subValue1",
        "subKey2":"subValue2"
    },
    "arrayOfSubObjects":[
        {"subKey3":"subValue3"},
        {"subKey4":"subValue4"}
    ]
}

您可以使用jsonObject.key引用JSON对象的任何元素,但请记住[]之间的部分是数组,因此您需要将它们作为数组中的索引,因此:

// to point subKey1:
jsonObject.subObject.subKey1;
// to point subKey3
jsonObject.arrayOfSubObjects[0].subKey3;
OR
// to point subKey1:
jsonObject["subObject"]["subKey1"];
// to point subKey3
jsonObject["arrayOfSubObjects"][0]["subKey3"];

请注意,0没有引号,因为它是一个索引。