【问题标题】:Build php array based on children from multidimensional array基于多维数组中的子级构建php数组
【发布时间】:2018-10-31 14:55:18
【问题描述】:

我有一个这样的 php 数组:

$arr = array(
    0 => array(
        "text" => "eventyrer",
        "children"=> array(
                4 => array(
                        "text" => "news",
                        "children"=> array(
                                1=> array("text"=>"a")
                            )
                    ),

                5 => array(
                        "text" => "nyheter",
                        "children"=> array(
                                1=> array("text"=>"b")
                            )
                    )
            ) 
    ),

    1 => array(
        "text" => "eventyrer2017",
        "children"=> array(
                6 => array(
                        "text" => "news",
                        "children"=> array(
                                1=> array("text"=>"c")
                            )
                    ),

                8 => array(
                        "text" => "nyheter",
                        "children"=> array(
                                1=> array("text"=>"d")
                            )
                    )
            ) 
    )

);

我怎样才能得到这样的输出:

$array = array(
    0 => "eventyrer/news/a",
    1 => "eventyrer/nyheter/b",
    2 => "eventyrer2017/news/c",
    4 => "eventyrer2017/nyheter/d",
)

这里我需要获取“文本”,然后附加“/”,然后通过“孩子”获取他们的文本。 子文本将与父文本一起添加。

【问题讨论】:

  • 会不会有多个孩子?
  • 您尝试过什么吗?您是否检查过 SO 上的其他预先存在的页面?你需要无限递归还是你的输入数组的深度是已知的?
  • 您能否将该数组粘贴为 JSON 编码字符串,以便我在此端解码和使用它?
  • 哦,是的,对不起,我以为是 print_r 输出一分钟,抱歉,哈哈:-P

标签: php arrays multidimensional-array parent children


【解决方案1】:

以下代码尝试递归方法,并为children 中的每个child 附加text 的每个段。这使得您的数据结构可以拥有无​​限的深度。

function flatten($arr) {
    $lst = [];
    /* Iterate over each item at the current level */
    foreach ($arr as $item) {
        /* Get the "prefix" of the URL */
        $prefix = $item['text'];
        /* Check if it has children */
        if (array_key_exists('children', $item)) {
            /* Get the suffixes recursively */
            $suffixes = flatten($item['children']);
            /* Add it to the current prefix */
            foreach($suffixes as $suffix) {
                $url = $prefix . '/' . $suffix;
                array_push($lst, $url);
            }
        } else {
            /* If there are no children, just add the 
             * current prefix to the list */
            array_push($lst, $prefix);
        }
    }

    return $lst;
}

输出

Array
(
    [0] => eventyrer/news/a
    [1] => eventyrer/nyheter/b
    [2] => eventyrer2017/news/c
    [3] => eventyrer2017/nyheter/d
)

【讨论】:

    【解决方案2】:

    我可以通过 3 个嵌套循环来做到这一点。

    //initialize new array to hold newly generated strings
    $new_array = [];
    
    foreach($arr as &$value) {
        //loop through first set
    
        foreach($value['children'] as $child) {
    
            //loop through children, create a string that contains the first set of values "text", and the childrens "text"
            $string = "{$value['text']}/{$child['text']}";
    
            foreach($child['children'] as $child2) {
    
                //loop through each child of child1 and add a new string to the new_array containing `$string` + the child2.
                $new_array[] = $string .= "/{$child2['text']}"; 
    
            }
        }
    
    }
    

    输出:

    Array ( [0] => eventyrer/news/a [1] => eventyrer/nyheter/b [2] => eventyrer2017/news/c [3] => eventyrer2017/nyheter/d )
    

    【讨论】:

    • 如果有人想知道,这仅适用于发布的数组深度。
    • @mickmackusa 是的,但是对于这个深度,您可以拥有任意数量的条目。
    • 是的,这就是它的灵活性。如果数组的“深度”完全改变,那就不合适了。
    • @mickmackusa True
    猜你喜欢
    • 2023-01-17
    • 2023-03-21
    • 2022-01-01
    • 2021-10-08
    • 2013-10-26
    • 2014-02-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    相关资源
    最近更新 更多