【问题标题】:Parsing multidimensional php array recursively, make a change and return a result递归解析多维php数组,进行更改并返回结果
【发布时间】:2016-10-09 11:21:46
【问题描述】:

我想解析一个数组,获取一些信息并返回一个新数组,但递归我不知道该怎么做。

这里是数组的例子:

$arr= array ( 
                0 => array ( 
                            'id'=> 0,
                            'dir_name' => '', 
                            'children' => array ( 
                                36 => array ( 
                                                'id'=>36,
                                                'dir_name' => 'dir 36', 
                                                'children' => array ( 
                                                    92 => array ( 
                                                                    'id'=>92,
                                                                    'dir_name' => 'dir 92', 
                                                                    'children' => array ( 
                                                                        93 => array ( 
                                                                                        'id'=>93,
                                                                                        'dir_name' => 'dir 93', ), ), ), 
                                                    94 => array ( 
                                                                    'id'=>94,
                                                                    'dir_name' => 'dir 94', 
                                                                    'children' => array ( 
                                                                        95 => array ( 
                                                                                        'id'=>95,
                                                                                        'dir_name' => 'dir 95', ), ), ), ), ), ), ), );

这是我的功能:

function all_dir($array,$id_selected=0,$pos=0,$tab_pos=[]) 
{
  $flat= [];

  foreach($array as $k=>$value) 
  {

    if (is_array($value)) 
        $flat = array_merge($flat, all_dir($value, $id_selected,$pos,$tab_pos));
    else 
    {
        if($k=="id")
        {
            $option='<option value="'.$value.'"';
            if($value==$id_selected)
                $option .=" selected ";

        }

        if($k=="dir_name")
        {
            $flat[] = $value;
            $tab_pos[$pos]=$value;

            $val='';
            for($i=0; $i<=$pos; $i++)
            {
                $val .= $tab_pos[$i].'/';
            }

            $option .='>'.$val.'</option>';

            echo $option;

            $pos++;
        }


    }

  }
  return $flat;
}

一个示例 html 代码:

<html>
    <body>

        <select name="" id="">
            <?php
                all_dir($arr, 93);
            ?>
        </select>
    </body>
</html>

我得到了什么:

<select id="" name="">
<option value="0">/</option>
<option value="36">/dir 36/</option>
<option value="92">/dir 36/dir 92/</option>
<option selected="" value="93">/dir 36/dir 92/dir 93/</option>
<option value="94">/dir 36/dir 94/</option>
<option value="95">/dir 36/dir 94/dir 95/</option>
</select>

我想要什么:

all_dir() 返回一个类似的数组(并且不显示一些选项 html 代码):

 [
        "0" =>"/",
        "36" =>"/dir 36/",
        "92" =>"/dir 36/dir 92/",
        "93" =>"/dir 36/dir 92/dir 93/",
        "94" =>"/dir 36/dir 94/",
        "95" =>"/dir 36/dir 94/dir 95/",
]

【问题讨论】:

  • 尝试使用深度优先搜索 (DFS)
  • 我解析数组没有问题,但是如何在递归函数中返回我的最终数组 :)

标签: php arrays recursion multidimensional-array


【解决方案1】:

您想将一种数组结构转换为另一种。它可以通过多种方式完成。

我决定将您的函数一分为二,第一个 get_dir 递归遍历数组并获取重要数据(iddir_name),第二个 get_dir_root 将立即格式转换为您期望的格式。

我还删除了$id_selected$pos$tab_pos 参数,因为您不在新版本中使用它。

function get_dir($array, $names = [])
{
    $ret = [];

    foreach ($array as $k => $v)
    {
        $id = $v['id'];
        $name = $v['dir_name'];

        $ret[$id] = $names;
        $ret[$id][] = $name;

        if (!empty($v['children']))
        {
            $xret = get_dir($v['children'], $ret[$id]);

            foreach ($xret as $kk => $vv)
            {
                $ret[$kk] = $vv;
            }
        }
    }

    return $ret;
}

function get_dir_root($array)
{
    $ret = [];

    foreach (get_dir($array) as $k => $v)
    {
        $ret[$k] = implode ('/', $v);

        if ($ret[$k] == '')
            $ret[$k] = '/';
        else
            $ret[$k] .= '/';
    }

    return $ret;
}

Working example

【讨论】:

  • 分成 2 个功能是关键。感谢您的帮助和解释:)
猜你喜欢
  • 2014-10-31
  • 1970-01-01
  • 1970-01-01
  • 2019-06-26
  • 2017-02-26
  • 1970-01-01
  • 2012-06-24
  • 2012-07-01
  • 2016-06-05
相关资源
最近更新 更多