PHP 多维数组访问 - 五维
PHP Multidimensional Array Access - Five Dimensions
关于我为什么这样做的参考,我正在尝试使用 SOAP/AXL WSDL API。我现在正在处理的那部分工作是以我可以访问它的方式构建数组结构。
此处 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
来遍历每个数组成员并能够 return 一个 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")
)))),
根据 Cisco 文档,CSS 包含以下元素:
- 姓名
- 说明
- 成员
成员本身包含以下多个键值对:
- 索引
- RouteParitionName
问题
- 数组结构是否正确?我想我这样做是因为每个 "Member" 由于键值对冲突而需要成为一个数组。
- 如果可能的话,如何使用 foreach 循环访问每个 KVP?
- 我是不是完全错了?
测试
所以我开始取得一些进步,但我有点迷茫。由于数组有 5 个级别,现在我必须循环 5 次才有意义。我不确定如何只提取我感兴趣的 KVP。
半工作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>");
}
}
}
}
}
更新
还有更多进展。我意识到我有一个数组只包含一个数组项。如此毫无意义。一旦我删除它似乎更有意义。我现在正在进行更多测试。更新代码:
$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>");
}
}
最终修复是以正确顺序获取嵌套 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();
}
关于我为什么这样做的参考,我正在尝试使用 SOAP/AXL WSDL API。我现在正在处理的那部分工作是以我可以访问它的方式构建数组结构。
此处 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
来遍历每个数组成员并能够 return 一个 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")
)))),
根据 Cisco 文档,CSS 包含以下元素:
- 姓名
- 说明
- 成员
成员本身包含以下多个键值对:
- 索引
- RouteParitionName
问题
- 数组结构是否正确?我想我这样做是因为每个 "Member" 由于键值对冲突而需要成为一个数组。
- 如果可能的话,如何使用 foreach 循环访问每个 KVP?
- 我是不是完全错了?
测试 所以我开始取得一些进步,但我有点迷茫。由于数组有 5 个级别,现在我必须循环 5 次才有意义。我不确定如何只提取我感兴趣的 KVP。
半工作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>");
}
}
}
}
}
更新
还有更多进展。我意识到我有一个数组只包含一个数组项。如此毫无意义。一旦我删除它似乎更有意义。我现在正在进行更多测试。更新代码:
$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>");
}
}
最终修复是以正确顺序获取嵌套 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();
}