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

4
  • 1
    what does "based on above array" means? Commented Apr 15, 2014 at 9:54
  • foreach($array as $var) {$yourOtherArray[] = $var;} Commented Apr 15, 2014 at 9:54
  • Take a look here. The only difference is that each of your args is just an element of the first array: stackoverflow.com/questions/2815162/… Commented Apr 15, 2014 at 9: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. Commented Apr 15, 2014 at 9:56

3 Answers 3

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;
     }
Sign up to request clarification or add additional context in comments.

Comments

3

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

6 Comments

Can you tell me what he wants to do?
Looks like he needs array of arrays where each "sub-array" holds the elements that have same index in source array.
Up gradation is really a solution?
Exactly what @DKasipovic said. OP is trying to get the columns of the array.
@DushyantJoshi YES! You should always run the latest (stable) versions of things, if for nothing else than for the security fixes, but mostly for all the new features and improvements!
|
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)

1 Comment

The whole condition block can be safely removed. 3v4l.org/GTjk1

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.