Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:通过键连接嵌套子数组的值_Php_Arrays - Fatal编程技术网

PHP:通过键连接嵌套子数组的值

PHP:通过键连接嵌套子数组的值,php,arrays,Php,Arrays,在过去的几天里,我一直对以下问题感到困惑,似乎无法想出一个优雅的解决方案 我有一个这样的数组,其中每个子数组都有一个以哈希符号为前缀的键,每个子数组都有一个不同的嵌套深度: Array( [#a1] => Array( [key1] => a1-1 [key2] => a1-2 [#b1] => Array( [key1] => b1-1

在过去的几天里,我一直对以下问题感到困惑,似乎无法想出一个优雅的解决方案

我有一个这样的数组,其中每个子数组都有一个以哈希符号为前缀的键,每个子数组都有一个不同的嵌套深度:

Array(
    [#a1] => Array(
            [key1] => a1-1
            [key2] => a1-2
            [#b1] => Array(
                    [key1] => b1-1
                    [key2] => b1-2
                )
            [#b2] => Array(
                    [key1] => b2-1
                    [key2] => b2-2
                    [#c1] => Array(
                            [key1] => c1-1
                            [key2] => c1-2
                        )
                    [#c2] => Array(
                            [key1] => c2-1
                            [key2] => c2-2
                        )
                )
        )
    [#a2] => Array(...)
)
我需要以某种方式遍历每个可能的嵌套路由,例如a1-b2-c1,但也要遍历a1-b2-c2,并在路由期间将相等的键合并到特定于该路由的数组中。结果数组的键并不重要

大概是这样的:

Array(

    // Follow path #a1 #b1
    [] = Array(
            [key1] = Array(a1-1, b1-1)
            [key2] = Array(a1-2, b1-2)
        )

    // Follow path #a1 #b2 #c1
    [] = Array(    
            [key1] = Array(a1-1, b2-1, c1-1)
            [key2] = Array(a1-2, b2-2, c1-2)
        )

    // Follow path #a1 #b2 #c2
    [] = Array(    
            [key1] = Array(a1-1, b2-1, c2-1)
            [key2] = Array(a1-2, b2-2, c2-2)
        )

    // Follow path #a2 ...
    [] = Array(...)

    )
)
我一直在考虑数组\合并\递归、数组\遍历\递归、递归foreaches,但无法将它们组合到实际工作的东西中

非常感谢您的帮助!谢谢

尝试使用usort,例如:

function compare_id($a, $b) {
    if ($a['id'] == $b['id']) return 0;
    return ($a['id'] < $b['id']) ? -1 : 1;
}

$a = [
    ['id' => 3, 'item' => 'pc'],
    ['id' => 1, 'item' => 'mouse'],
    ['id' => 2, 'item' => 'kb'],
];

usort($a, 'compare_id');

var_dump($a);   

我想知道这将如何生成所需的输出?