0

Sorry for the title. its little difficult for me to explain. I've spent almost few hours to figure this out, but failed. So I'm posting it here.

I have following array

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

    [1] => Array
        (
            [0] => p
            [1] => q
            [2] => r
        )

    [2] => Array
        (
            [0] => w
            [1] => x
            [2] => y
            [3] => z
        )

)

The array could have any number of elements.

What i need to do is create another array based on above array.

Array
(
    [0] => Array
        (
            [0] => a
            [1] => p
            [2] => w
        )

    [1] => Array
        (
            [0] => b
            [1] => q
            [2] => x
        )

    [2] => Array
        (
            [0] => c
            [1] => r
            [2] => y
        )

    [3] => Array
        (
            [0] => Z
        )

)

Any hints will be appreciated.

Thanks

Anand Solanki
  • 3,419
  • 4
  • 16
  • 27
WatsMyName
  • 4,240
  • 5
  • 42
  • 73
  • 1
    what does "based on above array" means? – Xatenev Apr 15 '14 at 09:54
  • foreach($array as $var) {$yourOtherArray[] = $var;} – Xatenev Apr 15 '14 at 09:54
  • Take a look here. The only difference is that each of your args is just an element of the first array: http://stackoverflow.com/questions/2815162/is-there-a-php-function-like-pythons-zip – Rohan Prabhu Apr 15 '14 at 09:56
  • from where I see it 2 nested loops could do the trick. If you could show us some code produced during the almost few hours you tried, that may help us finding where you're exactly struggling. – Laurent S. Apr 15 '14 at 09:56

3 Answers3

3

Upgrade to PHP 5.5 if you aren't already on it, then use array_column

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
3

If PHP < 5.5 or you don't want to use array_column-solution

$newArray = array();

foreach($array as $row)
     foreach($row as $key => $value){
         if (!isset($newArray[$key]))
             $newArray[$key] = array();
         $newArray[$key][] = $value;
     }
Alex
  • 8,055
  • 7
  • 39
  • 61
2

Just try with array_walk_recursive:

$input  = [
    ['a', 'b', 'c'],
    ['p', 'q', 'r'],
    ['w', 'x', 'y', 'z'],
];
$output = [];


array_walk_recursive($input, function($value, $index) use (&$output) {
    if (!isset($output[$index])) {
        $output[$index] = [];
    }

    $output[$index][] = $value;
});

Output:

array (size=4)
  0 => 
    array (size=3)
      0 => string 'a' (length=1)
      1 => string 'p' (length=1)
      2 => string 'w' (length=1)
  1 => 
    array (size=3)
      0 => string 'b' (length=1)
      1 => string 'q' (length=1)
      2 => string 'x' (length=1)
  2 => 
    array (size=3)
      0 => string 'c' (length=1)
      1 => string 'r' (length=1)
      2 => string 'y' (length=1)
  3 => 
    array (size=1)
      0 => string 'z' (length=1)
hsz
  • 148,279
  • 62
  • 259
  • 315