2

I have problem to add array into array. I cannot resolve it.

I have this array $A:

Array
(
    [1001] => Array
            (
                [0] => Array
                    (
                        [name] => 'Joe'
                        [surname] => 'Doe'
                        [age]  => 20
                        [height] => 180
                        [weight] => 80
                     )
            )
)

And I have this array $B:

Array
(
    [height] => 200
    [weight] => 100
)

How to create new array to get this result:

Array
(
    [1001] => Array
            (
                [0] => Array
                    (
                        [name] => 'Joe'
                        [surname] => 'Doe'
                        [height] => 180
                        [weight] => 80
                        [age]  => 20
                     )
            )
    [1001] => Array
            (
                [1] => Array
                    (
                        [name] => 'Joe2'
                        [surname] => 'Doe2'
                        [height] => 200
                        [weight] => 100
                        [age]  => 22
                     )
            )
)

I use this, but the result is not correct:

$array[1001][] = [
            'name'   => 'Joe2',
            'surname'=> 'Doe2',
            $B,
            'age' => 22
        ];

Thank you for answer.

4 Answers 4

4

You can use the operator + to merge your arrays:

$array = array(
    1001 => array(
        array(
            'name' => 'Joe',
            'surname' => 'Doe',
            'height' => 180,
            'weight' => 80,
            'age'  => 20
        )
    ),
);

$B = array('height' => 200, 'weight' => 100);

$array[1001][] = [
            'name'   => 'Joe2',
            'surname'=> 'Doe2',
            'age' => 22
        ] + $B;

print_r($array);

Will outputs:

Array
(
    [1001] => Array
        (
            [0] => Array
                (
                    [name] => Joe
                    [surname] => Doe
                    [height] => 180
                    [weight] => 80
                    [age] => 20
                )

            [1] => Array
                (
                    [name] => Joe2
                    [surname] => Doe2
                    [age] => 22
                    [height] => 200
                    [weight] => 100
                )

        )

)
Sign up to request clarification or add additional context in comments.

3 Comments

] + $B; ... I didn't even know you could do that with arrays :D
How simple. Thank you very much.
@Syscall ... knew about comparison, just it never even occurred to me to consider joining 2 arrays with + before.
3

Try array_merge():

$array[1001][] = array_merge(['name'=>'Joe2','surname'=> 'Doe2','age' => 22],$B);

OR

$array[1001][] = ['name'=>'Joe2','surname'=> 'Doe2','age' => 22] + $B;

Comments

3

You can use array_merge like this:

$array[1001][] = array_merge([
    'name'   => 'Joe2',
    'surname'=> 'Doe2',
    'age' => 22
], $B);

Comments

0
$innerArray = array();
$item1Array = array();
array_push($item1Array,"Data 1");
array_push($item1Array,"Data 2");
$item2Array = array();
array_push($item2Array,"Data 3");
array_push($item2Array,"Data 4");
array_push($innerArray, $item1Array);
array_push($innerArray, $item2Array);
$result = array();
$result["result"] = "Success";
$result["user"] = $innerArray;

Result :

{
    "result" => "Success",
    "user" => [
        [0] =>[
            [0]=>"Data 1",
            [1]=>"Data 1",
        ],
        [1] =>[
            [0]=>"Data 3",
            [1]=>"Data 4",
        ]
    ]
}

Hope it helps.

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.