0

I'm getting following kind of array while i loop the contents i need to convert this array format to the following format , This below one is the actual array i am getting while i loop.

Array
(
    [jasmine] => Array
        (
            [0] => a
            [1] => bb
            [2] => c
            [3] => d
        )

)
Array
(
    [rose] => Array
        (
            [0] => a
            [1] => 4
            [2] => 0.5
            [3] => d
        )

)
Array
(
    [strawberry] => Array
        (
            [0] => a
            [1] => 42
            [2] => 0.5
            [3] => f
        )
)

And the Resultant array which i need is the below one how can i achieve this any helps appreciated. Thanks in advance.

Array(
    [jasmine] => Array
        (
            [0] => a
            [1] => bb
            [2] => c
            [3] => d
        )
    [rose] => Array
        (
            [0] => a
            [1] => 4
            [2] => 0.5
            [3] => d
        )
    [strawberry] => Array
        (
            [0] => a
            [1] => 42
            [2] => 0.5
            [3] => f
        )
);

1 Answer 1

1

First, I put all the arrays in the $arrays var, for reading purposes:

$arrays = array(
    array(
        'jasmine' => array('a','bb','c','d')
    ),
    array(
        'rose' => array('a','4','0.5','d')
    ),
    array(
        'strawberry' => array('a','42','0.5','f')
    )
);

Then an array merge will do it:

$result = array_merge($arrays[0],$arrays[1],$arrays[2]);

The result will be (var_dump):

array(3) { ["jasmine"]=> array(4) { [0]=> string(1) "a" [1]=> string(2) "bb" [2]=> string(1) "c" [3]=> string(1) "d" } ["rose"]=> array(4) { [0]=> string(1) "a" [1]=> string(1) "4" [2]=> string(3) "0.5" [3]=> string(1) "d" } ["strawberry"]=> array(4) { [0]=> string(1) "a" [1]=> string(2) "42" [2]=> string(3) "0.5" [3]=> string(1) "f" } }
Sign up to request clarification or add additional context in comments.

Comments

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.