Blogger Information
Blog 4
fans 0
comment 0
visits 3646
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
在多维数组中,搜索子多维数组
GTOFFICE的博客
Original
882 people have browsed it
<?php
//多维数组中,根据传入的ID,获取该ID的所有子多维数组
//$list 传入的数组
//$str  ID
//如果把返回值不放在函数外部,就会得到null
//只要匹配到一次,那就直接数组复制,把子结点也复制进来了,赋给返回值,就是你要的无限层级查找结果
$_result = array();
function getArr($_list, $str){
  global $_result; //引入外部定义的变量
  if(is_array($_list)){
    for($_i=0; $_i<sizeof($_list); $_i++) {
      if ($_list[$_i]["id"] == $str) {
        $_result = $_list[$_i];
          echo "找到了节点";
          break;
      } else {
        //不匹配, 继续查找是否有子结点
        if (isset($_list[$_i]['children']) && is_array($_list[$_i]['children'])) {
          getArr($_list[$_i]["children"], $str); //有子结点, 递归调用
        }
      }
    }
  }
}
//执行循环
getArr($_arr, "12104");
var_dump($_result);
?>


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post