How do you iterate through a structured array (that is, a multidimensional array)?

Source: Internet
Author: User

I want this effect, in an iterative way

I now have this effect, how do I get the array to be taken out of the hierarchy?

Current code, how to modify it? Or give it a thought.

//迭代调用子孙树function node_merge($node,$access,$parent = 0){    $task = array($parent) ; //任务表    $arr = array(); //返回数据    while (!empty($task)) {        $flag = false; //声明标识符        foreach ($node as $k=> $v) {            if($v['pid'] == $parent){                if (isset($node[$parent])) {                    $arr[] = $v;                }                // $arr[] = $v; //将找到的栏目存进$arr,等待返回                array_push($task, $v['id']);//将最新的地区存入任务栈,只要task还有任务,循环会继续.                $parent = $v['id'];//将当前id存入$parent                unset($node[$k]); //把找到的单元unset掉                $flag = true; //说明找到了子栏目            }            }        //如果flsg为false,就代表找不到已找不到子栏目了,可以将最后的单元pop掉然后继续循环了.        if ($flag == false) {            array_pop($task); //弹出任务表                $parent = end($task); //把找到的单元unset掉            }            }        return $arr;}

Reply content:

I want this effect, in an iterative way

I now have this effect, how do I get the array to be taken out of the hierarchy?

Current code, how to modify it? Or give it a thought.

//迭代调用子孙树function node_merge($node,$access,$parent = 0){    $task = array($parent) ; //任务表    $arr = array(); //返回数据    while (!empty($task)) {        $flag = false; //声明标识符        foreach ($node as $k=> $v) {            if($v['pid'] == $parent){                if (isset($node[$parent])) {                    $arr[] = $v;                }                // $arr[] = $v; //将找到的栏目存进$arr,等待返回                array_push($task, $v['id']);//将最新的地区存入任务栈,只要task还有任务,循环会继续.                $parent = $v['id'];//将当前id存入$parent                unset($node[$k]); //把找到的单元unset掉                $flag = true; //说明找到了子栏目            }            }        //如果flsg为false,就代表找不到已找不到子栏目了,可以将最后的单元pop掉然后继续循环了.        if ($flag == false) {            array_pop($task); //弹出任务表                $parent = end($task); //把找到的单元unset掉            }            }        return $arr;}

Shouldn't be so complicated
This can be easily achieved by passing the following:

The

function parseTree($tree, $root = null) {    $nested = array();    foreach($tree as $index => $child) {        if($child['pid'] == $root) {            unset($tree[$index]);            $child['children'] = parseTree($tree, $child['id']);            $nested[] = $child;        }    }    return empty($nested) ? null : $nested;}

Iteration

function parseTree($rows) {  $rows = array_column ( $rows, null, 'id' );  foreach ( $rows as $key => $val ) {      if ( $val ['pid'] ) {          if ( isset ( $rows [$val ['pid']] )) {              $rows [$val ['pid']]['children'][] = &$rows [$key];          }      }  }  foreach ( $rows as $key => $val ) {      if ( $val ['pid'] ) unset ( $rows [$key] );  }  return $rows;}
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.