phparrayssoapcisco-axl

PHP Multidimensional Array Access - Five Dimensions


For reference on why I'm doing this, I'm trying to use a 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. 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

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. So a complete CSS would be the following:

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:

Members itself contains multiple Key values pairs of the following:

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?
  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. What I'm not sure how to do is extract only the KVPs I'm interested in.

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

Solution

  • Final fix was getting items in the nested foreach loop in the correct order.

        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();
        }