1

I have an array at present that looks like this:

Array
(
    [0] => Array
        (
            [language] => English
        )

    [1] => Array
        (
            [language] => Arabic
        )

    [2] => Array
        (
            [language] => Bengali
        )
)

What I'd like to do is change it so it looks like this:

Array
(
    [language] => Array
        (
            [0] => English
            [1] => Arabic
            [2] => Bengali
        )
)

I also have an array that looks like this:

Array
(
       [id] => 3
       [name] => lethalMango
       [joined] => 2010-01-01 00:00:00
)

And I'd like to change it to:

Array
(
    [user] => Array
        (
           [id] => 3
           [name] => lethalMango
           [joined] => 2010-01-01 00:00:00
        )
)

I've tried a number of methods without much success but I'm sure there is an more efficient way.

1
  • It's hard to compare the efficiency of our proposals with your code given that you don't share it with us... Commented May 11, 2011 at 9:49

2 Answers 2

4
FIRST : 

$result = array();
foreach($array as $value){
  $result['language'][]= $value['language']
}



SECOND : 

 $result['user'] = $array;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much :) Works a treat
1

Second

$result = array('user'=>$array);

1 Comment

hehe. Gaurav wrote the solution for the first array first and then edited the second one into it ;) However, this one is slightly different and it doesnt require $result to exists before the array is assigned, so I keep the answer :)

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.