PHP 多维数组路径段组合循环


php multidimensional array path segment combination loop

我正在尝试找到一种方法来使其工作。但我很难思考逻辑。

我有这个数组:

Array
(
    [0] => Array
        (
            [0] => news
            [1] => {section}
            [2] => {slug}
            [3] => {*}
        )
    [1] => Array
        (
            [0] => {id}
            [1] => {*}
        )
    [2] => Array
        (
            [0] => {date}
            [1] => 25-07-1982
            [2] => {section}
            [3] => {slug}
            [4] => {*}
        )
)

我需要转换为此结果:

0 news/{id}/{date}
1 news/{id}/25-07-1982
2 news/{id}/{section}
3 news/{id}/{slug}
4 news/{id}/{*}
5 news/{*}/{date}
6 news/{*}/25-07-1982
7 news/{*}/{section}
8 news/{*}/{slug}
9 news/{*}/{*}
10 {section}/{id}/{date}
11 {section}/{id}/25-07-1982
12 {section}/{id}/{section}
13 {section}/{id}/{slug}
14 {section}/{id}/{*}
15 {section}/{*}/{date}
16 {section}/{*}/25-07-1982
17 {section}/{*}/{section}
18 {section}/{*}/{slug}
19 {section}/{*}/{*}
20 {slug}/{id}/{date}
21 {slug}/{id}/25-07-1982
22 {slug}/{id}/{section}
23 {slug}/{id}/{slug}
24 {slug}/{id}/{*}
25 {slug}/{*}/{date}
26 {slug}/{*}/25-07-1982
27 {slug}/{*}/{section}
28 {slug}/{*}/{slug}
29 {slug}/{*}/{*}
30 {*}/{id}/{date}
31 {*}/{id}/25-07-1982
32 {*}/{id}/{section}
33 {*}/{id}/{slug}
34 {*}/{id}/{*}
35 {*}/{*}/{date}
36 {*}/{*}/25-07-1982
37 {*}/{*}/{section}
38 {*}/{*}/{slug}
39 {*}/{*}/{*}

输入数组可能包含三个以上的键,因此我正在寻找的解决方案应该是动态的。结果应与上面显示的结果具有相同的顺序。有人知道如何以有效的方式做到这一点吗?有人可以推动我朝着正确的方向前进吗?多谢!:)

像这样

foreach ($array[0] as $val0 ) 
  foreach ($array[1] as $val1 ) 
    foreach ($array[2] as $val2 ) 
        $newArray[] = "$val0/$val1/$val2";

编辑:对于可变数组长度

function recursive($array , $length = 0){
  $retval =array();
  if($length < count($array) -1){
    foreach ($array[$length] as $val0 ) 
        foreach (recursive($array, $length+1) as $val1)
            $retval[] =  "$val0/$val1";
  }
  else
  {
     foreach ($array[$length] as $val0 ) 
        $retval[] =  "$val0";
  }
  return $retval;
}
print_r(recursive($array));

仅仅因为我喜欢编写错误/管理 PHP 数组的函数,我就把它放在一起,主要是因为我非常确定你可以避免递归——因为结构本身不是递归的。(我的头脑似乎认为这是一个规则,我相信某个地方的某个人可以证明它是错的)。

foreach ( array_reverse($array) as $sub ) {
  if ( isset($rem) ) {
    $ret = array();
    foreach ( $sub as $itm ) {
      foreach ( $rem as $val ) { $ret[] = "$itm/$val"; }
    }
    $rem = $ret;
  }
  else {
    $rem = $sub;
  }
}

$rem中找到的输出如下所示:

Array (
  [0] => news/{id}/{date}
  [1] => news/{id}/25-07-1982
  [2] => news/{id}/{section}
  [3] => news/{id}/{slug}
  [4] => news/{id}/{*}
  [5] => news/{*}/{date}
  [6] => news/{*}/25-07-1982
  [7] => news/{*}/{section}
  [8] => news/{*}/{slug}
  [9] => news/{*}/{*}
  [10] => {section}/{id}/{date}
  [11] => {section}/{id}/25-07-1982
  [12] => {section}/{id}/{section}
  [13] => {section}/{id}/{slug}
  [14] => {section}/{id}/{*}
  [15] => {section}/{*}/{date}
  [16] => {section}/{*}/25-07-1982
  [17] => {section}/{*}/{section}
  [18] => {section}/{*}/{slug}
  [19] => {section}/{*}/{*}
  [20] => {slug}/{id}/{date}
  [21] => {slug}/{id}/25-07-1982
  [22] => {slug}/{id}/{section}
  [23] => {slug}/{id}/{slug}
  [24] => {slug}/{id}/{*}
  [25] => {slug}/{*}/{date}
  [26] => {slug}/{*}/25-07-1982
  [27] => {slug}/{*}/{section}
  [28] => {slug}/{*}/{slug}
  [29] => {slug}/{*}/{*}
  [30] => {*}/{id}/{date}
  [31] => {*}/{id}/25-07-1982
  [32] => {*}/{id}/{section}
  [33] => {*}/{id}/{slug}
  [34] => {*}/{id}/{*}
  [35] => {*}/{*}/{date}
  [36] => {*}/{*}/25-07-1982
  [37] => {*}/{*}/{section}
  [38] => {*}/{*}/{slug}
  [39] => {*}/{*}/{*}
)

此外,对于那些喜欢他们的数组多维的人来说,这可能会派上用场(尽管我不想考虑这样一个代码高尔夫版本的开销是多少)。需要明确的是,第二个示例不会按照 OP 的请求创建字符串列表,而是创建分层数组结构。

foreach ( array_reverse($array) as $sub ) {
  $rem = isset($rem)
    ? array_combine($sub, array_fill(0, count($sub), $rem))
    : $sub
  ;
}

这将生成(再次在$rem):

Array (
  [news] => Array (
      [{id}] => Array (
          [0] => {date}
          [1] => 25-07-1982
          [2] => {section}
          [3] => {slug}
          [4] => {*}
        )
      [{*}] => Array (
          [0] => {date}
          [1] => 25-07-1982
          [2] => {section}
          [3] => {slug}
          [4] => {*}
      )
    )
  [{section}] => Array (
      [{id}] => Array (
          [0] => {date}
          [1] => 25-07-1982
          [2] => {section}
          [3] => {slug}
          [4] => {*}
        )
... and so on

现在,如果只有PHP有一个包含keys.
join_recursive(除了帮助上述内容外,这几乎毫无意义)。