简体   繁体   English

PHP多维数组访问-五个维度

[英]PHP Multidimensional Array Access - Five Dimensions

For reference on why I'm doing this, I'm trying to use a SOAP / AXL WSDL API. 有关为什么执行此操作的参考,我尝试使用SOAP / AXL WSDL API。 That portion I have working the callenge I have now is building the array construct in such a way I can access it. 我正在处理的部分我现在正在以一种可以访问它的方式构建数组构造。

For reference the API requirements are here. 供参考,此处是API要求。 The challenge I'm having is building the members array for each upper array element. 我面临的挑战是为每个上部数组元素构建成员数组。 https://developer.cisco.com/media/axl-schema-11-0/Files/AXLSoap_AddCssReq.html#Link68 https://developer.cisco.com/media/axl-schema-11-0/Files/AXLSoap_AddCssReq.html#Link68

Array Construct: 数组构造:

    `$cssnames = array(
                array("name"=>"US-420-blah Gateway",
                        "description"=>"US-420 Gateway CSS",
                        "members"=>array(
                                        array(
                                            "member"=>array(
                                                "Index"=>"1",
                                                "Routeparition"=>"fancyParition"),
                                        array("member"=>array(
                                                "Index"=>"2",
                                                "Routeparition"=>"otherpartition")
                                            )))),
                array("name"=>"US-420-blah Internal Local DN CallFwd",
                        "description"=>"US-420 CSS for Call Forward",
                        "members"=>array(
                                        array(
                                            "member"=>array(
                                                "Index"=>"1",
                                                "Routeparition"=>"fancyParition"),
                                        array("member"=>array(
                                                "Index"=>"2",
                                                "Routeparition"=>"otherpartition")
                                            )))),
                array("name"=>"US-420-blah Internal Local LD DN CallFwd",
                        "description"=>"US-420 for Call Forward LD Allowed",
                        "members"=>array(
                                        array(
                                            "member"=>array(
                                                "Index"=>"1",
                                                "Routeparition"=>"fancyParition"),
                                        array("member"=>array(
                                                "Index"=>"2",
                                                "Routeparition"=>"otherpartition")
                                            )))),
                array("name"=>"US-420-blah Phones Device",
                        "description"=>"US-420 Device CSS",
                        "members"=>array(
                                        array(
                                            "member"=>array(
                                                "Index"=>"1",
                                                "Routeparition"=>"fancyParition"),
                                        array("member"=>array(
                                                "Index"=>"2",
                                                "Routeparition"=>"otherpartition")
                                            )))),
                array("name"=>"US-420-blah Phones Device Internal Only",
                        "description"=>"US-420 Device CSS Internal",
                        "members"=>array(
                                        array(
                                            "member"=>array(
                                                "Index"=>"1",
                                                "Routeparition"=>"fancyParition"),
                                        array("member"=>array(
                                                "Index"=>"2",
                                                "Routeparition"=>"otherpartition")
                                            )))));                          
foreach($cssnames['members'] as $items){
        echo($items['0']['member'] . "</br>");
        }`

The goal here would be able to user a foreach to be able to loop through each array member and able to return a whole value for one CSS. 这里的目标将是使用户可以使用foreach来遍历每个数组成员并返回一个CSS的整个值。 So a complete CSS would be the following: 因此,完整的CSS将如下所示:

array("name"=>"US-420-blah Gateway", "description"=>"US-420 Gateway CSS", "members"=>array( array( "member"=>array( "Index"=>"1", "Routeparition"=>"fancyParition"), array("member"=>array( "Index"=>"2", "Routeparition"=>"otherpartition") )))),

Per Cisco Documentation a CSS contains the following elements: 根据Cisco文档,CSS包含以下元素:

  • Name 名称
  • Description 描述
  • Members 会员

Members itself contains multiple Key values pairs of the following: 成员本身包含以下多个键值对:

  • Index 指数
  • RouteParitionName RouteParitionName

Questions 问题

  1. Do I have the array structure correct? 我的数组结构正确吗? I think I do as each "Member" needs to be an array due to colliding key value pairs. 我想我这样做是因为由于冲突的键值对,每个“成员”都需要一个数组。
  2. How do I access each KVP using a foreach loop if that's even possible? 如果可能的话,如何使用foreach循环访问每个KVP?
  3. Am I just going about this all wrong? 我只是想解决所有这些错误吗?

Testing So I'm starting to make some progress but I'm a bit lost. 测试所以我开始取得一些进步,但是我有点迷茫。 Since there are 5 levels to the array it makes sense now that I must loop 5 times. 由于数组有5个级别,所以我必须循环5次才有意义。 What I'm not sure how to do is extract only the KVPs I'm interested in. 我不确定该怎么做,只提取我感兴趣的KVP。

Semi working return. 半工作回报。

$cssnames = array("css_list"=>
                array("name"=>"US-420-blah Gateway",
                        "description"=>"US-420 Gateway CSS",
                        "members"=>array(
                                        array("member"=>array(
                                                "Index"=>"1",
                                                "Routeparition"=>"fancyParition"),
                                        array("member"=>array(
                                                "Index"=>"2",
                                                "Routeparition"=>"otherpartition")
                                            )))));

foreach($cssnames as $items=>$css){
    foreach ($css as $name=>$test){ 
        echo($test . "</br>");
        foreach($test as $memberlist=>$member){
            foreach($member as $memberitems=>$memberdetails){
                foreach($memberdetails as $details=>$config){
                    echo($config . "</br>");    
                }
            }
        }
    }
}

UPDATE 更新

So some more progress. 还有更多进展。 I realized that I had an array just holding a single array item. 我意识到我只有一个数组,只保存一个数组项。 So pointless. 没意义 Once I removed that it seems to make more sense. 一旦我删除了它,这似乎更有意义。 I'm now moving on to more testing. 我现在要进行更多测试。 Updated code: 更新的代码:

$cssnames = array("css_list"=>
                array("name"=>"US-420-blah Gateway",
                        "description"=>"US-420 Gateway CSS",
                        "members"=>array(
                                        array(
                                                "Index"=>"1",
                                                "Routeparition"=>"fancyParition"),
                                        array(
                                                "Index"=>"2",
                                                "Routeparition"=>"otherpartition")
                                            )));

foreach($cssnames as $level1){
    echo($level1['name'] . "</br>");
    echo($level1['description'] . "</br>");

        foreach ($level1['members'] as $level2){
            echo($level2['Index'] . "</br>");
            echo($level2['Routeparition'] . "</br>");
    }
}

Final fix was getting items in the nested foreach loop in the correct order. 最终解决方法是按正确的顺序在嵌套的foreach循环中获取项目。

    foreach($csslist as $level1) {
        $cssname = $level1['name'];
        $cssdescription = $level1['description'];

        foreach($level1['members'] as $level2){ 
            $members[] = array(
                                "index"=>$level2['index'],
                                "routePartitionName"=>$level2['routePartitionName']);
        }
        $programTags[] = array(
                            "name"=>"$cssname",
                            "description"=>"$cssdescription",
                            "members"=>$members);
        //Empty the Members array for the next loop iteration
        $members = array();
    }

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

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