Php 列出多维数组的所有路径 所以我得到了这个: 我正在寻找一种方法来获得这个输出(递归): 一 第二条 第三条 一个4›两个1 一个4›两个2›三个1 一个4›两个3 第五条 就我而言,我有这个功能

Php 列出多维数组的所有路径 所以我得到了这个: 我正在寻找一种方法来获得这个输出(递归): 一 第二条 第三条 一个4›两个1 一个4›两个2›三个1 一个4›两个3 第五条 就我而言,我有这个功能,php,Php,此输出包含以下路径: 一 第二条 第三条 一个4>2个1 一个4>2个3 第五条 它不应该找到一个4>两个2>三个1?可能会帮助您 function getKeyPaths(array $tree, $glue = '.') { $paths = array(); foreach ($tree as $key => &$mixed) { if (is_array($mixed)) { $results = getKeyPa

此输出包含以下路径:

  • 第二条
  • 第三条
  • 一个4>2个1
  • 一个4>2个3
  • 第五条
它不应该找到
一个4>两个2>三个1

可能会帮助您

function getKeyPaths(array $tree, $glue = '.')
{
    $paths = array();
    foreach ($tree as $key => &$mixed) {
        if (is_array($mixed)) {
            $results = getKeyPaths($mixed, $glue);
            foreach ($results as $k => &$v) {
                $paths[$key . $glue . $k] = $v;
            }
            unset($results);
        } else {
            $paths[$key] = $mixed;
        }
    }

    return $paths;
}

你尝试过什么?在哪里失败了?这不是一个“为我编写代码”的网站。大部分内容都是这样的:@jandogen抱歉……我在堆栈中尝试了一些解决方案,但其中许多解决方案,如#deceze所示,都是关于创建键的路径,而不是值。此刻,我已经融化了我的大脑很久,试图解决它,并要求一些帮助。真棒的解决方案!
function getValuesPaths(array $tree, $glue = ' > ') {
    $branches = array();
    foreach ($tree as &$item) {
        $piece = $item['name'];
        if (array_key_exists('children', $item)) {
            if (count($item['children'])>1) {
                $leafs = self::getValuesPaths($item['children']);
                foreach ($leafs as $item) {
                    $branches[] = $piece . $glue . $item;
                }
            }   
        } else {
            $branches[] = $piece;
        }
    }
    return $branches;
}
function getKeyPaths(array $tree, $glue = '.')
{
    $paths = array();
    foreach ($tree as $key => &$mixed) {
        if (is_array($mixed)) {
            $results = getKeyPaths($mixed, $glue);
            foreach ($results as $k => &$v) {
                $paths[$key . $glue . $k] = $v;
            }
            unset($results);
        } else {
            $paths[$key] = $mixed;
        }
    }

    return $paths;
}