0

I have an array like this, returned by MySQL:

array(0 => array('123', 'data'), 1 => array('124', 'data2'));

Now I want to transform it into this without doing any loops through an array:

array('123' => 'data', '124' => 'data2');
3
  • 1
    Any particular reason why no loops? Is this homework? or do you just want to make things harder? Commented Mar 13, 2012 at 22:05
  • Just for fun. It is easy to use a foreach loop for this, but it is actually boring. Commented Mar 13, 2012 at 22:08
  • Just for fun is a motive I can understand Commented Mar 13, 2012 at 22:13

1 Answer 1

1
$data = array(0 => array('123', 'data'), 1 => array('124', 'data2'));

$newdata = array_combine( array_map( function( $item ) {
                                         return $item[0];
                                     },
                                     $data
                                   ),
                          array_map( function( $item ) {
                                         return $item[1];
                                     },
                                     $data
                                   )
                        );

var_dump($newdata);
Sign up to request clarification or add additional context in comments.

1 Comment

Making trivial tasks look like l33t ;)

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.