PHP多维数组访问 - 五维

时间:2016-02-24 02:17:19

标签: php arrays soap cisco-axl

关于我为什么要这样做的参考,我正在尝试使用SOAP / AXL WSDL API。我现在使用callenge的部分是以我可以访问它的方式构建数组结构。

供参考,API要求在此处。我遇到的挑战是为每个上层数组元素构建成员数组。 https://developer.cisco.com/media/axl-schema-11-0/Files/AXLSoap_AddCssReq.html#Link68

阵列构造:

    `$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>");
        }`

此处的目标是能够使用foreach来遍历每个数组成员并能够返回一个CSS的整个值。所以一个完整的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") )))),

根据思科文档,CSS包含以下元素:

  • 姓名
  • 说明
  • 成员

成员本身包含以下多个键值对:

  • 索引
  • RouteParitionName

问题

  1. 我的阵列结构是否正确?我认为我这样做,因为每个“成员”都需要成为一个阵列,因为碰撞了键值对。
  2. 如果可能的话,如何使用foreach循环访问每个KVP?
  3. 我只是这样做错了吗?
  4. 测试 所以我开始取得一些进展,但我有点失落。由于数组有5个级别,因此我必须循环5次才有意义。我不知道该怎么做只提取我感兴趣的KVP。

    半工作回报。

    $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>");    
                    }
                }
            }
        }
    }
    

    更新

    因此取得了一些进展。我意识到我有一个数组只是持有一个数组项目。没有意义。一旦我删除它似乎更有意义。我现在正在进行更多测试。更新的代码:

    $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>");
        }
    }
    

1 个答案:

答案 0 :(得分:0)

最终修复是以正确的顺序在嵌套的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();
    }