2

I am trying to figure a way to get this to work. But I have a hard time thinking out the logics.

I have this array:

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] => {*}
        )

)

That I need to convert to this result:

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

The input array could contain more than three keys, so the solution I'm looking for should be dynamic. And the result should have the same order as the result shown above. Does someone know how to do this in a efficient way? Can someone give me a push in the right direction? Thanks a lot! :)

3
  • Put "permutations php" to google.com
    – zerkms
    Nov 21, 2014 at 8:42
  • I think that all you need are 3 nested loops
    – Serpes
    Nov 21, 2014 at 8:44
  • @zerkms Thanks for that term, I was struggling to name this issue to be able to Google for it! Nov 21, 2014 at 8:53

2 Answers 2

3

Sth like this

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

EDIT: for variable array length

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));
3
  • Ow that is in fact getting me there! Staring at this for an hour but couldn't see this :S. Time for weekend I think... The input array could also be more that three, but I think with some recursion I should be able to get this to work! Nov 21, 2014 at 8:51
  • Hmm, I can't make this recursive. Any ideas? Nov 21, 2014 at 9:43
  • I added recursive function Nov 21, 2014 at 13:08
1

Just because I like writing functions that mis/manage PHP arrays, I put this together, mainly because I was pretty sure you could avoid recursion — because the structure itself isn't recursive. (My head seems to think that is a rule, I'm sure someone somewhere can prove it wrong).

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

The output found in $rem is as follows:

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

Also, for those that like their arrays multidimensional, this might come in handy (although I'd hate to think what the overheads are for such a code golfed version). Just to be clear, this second example doesn't create the string list as requested by the OP, but a hierarchical array structure instead.

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

This generates (again in $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

Now if only PHP had a join_recursive that included keys.
(it would be almost pointless, save for helping with the above).

1
  • Oh! Great I'm only seeing this now. See if I can put this to some good use. Thanks! Nov 26, 2014 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.