【问题标题】:php multidimensional array path segment combination loopphp多维数组路径段组合循环
【发布时间】:2015-01-19 08:26:30
【问题描述】:

我正在想办法让它发挥作用。但我很难思考其中的逻辑。

我有这个数组:

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 {*}/{*}/{*}

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

【问题讨论】:

  • 把“permutations php”放到google.com
  • 我认为你只需要 3 个嵌套循环
  • @Serpes 是的,我现在看到了,谢谢!
  • @zerkms 感谢这个词,我一直在努力命名这个问题以便能够在谷歌上找到它!

标签: php loops multidimensional-array


【解决方案1】:

仅仅因为我喜欢编写错误/管理 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 有一个包含键的 join_recursive 就好了。
(这几乎是没有意义的,除了上面的帮助之外)

【讨论】:

  • 哦!太好了,我现在才看到这个。看看我能不能好好利用它。谢谢!
【解决方案2】:

这样的事

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));

【讨论】:

  • 哇,这实际上让我到了那里!盯着这个看了一个小时,但看不到这个:S。周末的时间我想......输入数组也可能超过三个,但我认为通过一些递归我应该能够让它工作!
  • 嗯,我不能让这个递归。有什么想法吗?
  • 我添加了递归函数
  • 非常感谢!你今天真的帮了我大忙!
猜你喜欢
  • 2013-06-25
  • 2019-07-17
  • 2012-05-13
  • 1970-01-01
  • 2018-03-24
  • 2013-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多