0

I have following array :

Array
(
    [0] => Array
        (
            [id] => 23
            [title] => jkl
        )

    [1] => Array
        (
            [id] => 478
            [title] => lmn
        )

    [2] => Array
        (
            [id] => 22
            [title] => pqr
        )

    [3] => Array
        (
            [id] => 21
            [title] => abc
        )
)

And same is the 2nd array :

Array
(
    [0] => Array
        (
            [id] => 103
            [title] => Activities
        )

    [1] => Array
        (
            [id] => 76
            [title] => Top 10 Ideas
        )

    [2] => Array
        (
            [id] => 9
            [title] => Best Shopping Areas
        )    
)

I want to append 1st array into 2nd array if count of 1st array is less than 3 so for that I used array push function and did like this :

if (count($secondArr) < 3) {
            echo "<pre>";
            echo array_push($secondArr, $firstArr);
            print_r($result);
            exit;
        }

Now after array_push it showing the array like this (multidimensional)

Array
(
    [0] => Array
        (
            [id] => 76
            [title] => Activities
        )

    [1] => Array
        (
            [id] => 103
            [title] => Top 10 Ideas
        )

    [2] => Array
        (
            [id] => 9
            [title] => Best Shopping Areas
        )

    [3] => Array
        (
            [0] => Array
                (
                    [id] => 23
                    [title] => jkl
                )

            [1] => Array
                (
                    [id] => 478
                    [title] => lmn
                )

            [2] => Array
                (
                    [id] => 22
                    [title] => pqr
                )

            [3] => Array
                (
                    [id] => 21
                    [title] => abc
                )   

        )

)

I dont want like this (multidimensional). I want it to append with key 4,5 and so on. Can it possible if yes then how?

Thanks.

7 Answers 7

2

You should use array_merge instead.

$result = array_merge($secondArr, $firstArr);
Sign up to request clarification or add additional context in comments.

Comments

1

array_push inserts one new element into the array. You want to merge two arrays:

$firstArray = array_merge($firstArray, $secondArray);

Comments

1

As according to the PHP.net specification for array_push:

array_push() treats array as a stack, and pushes the passed variables onto the end of array. The length of array increases by the number of variables pushed. Has the same effect as:

So it wouldn't work. You would want to use array_merge instead of array_push, in the exact same way.

Comments

1

Use array_merge

https://www.php.net/manual/en/function.array-merge.php

array_push — Push one or more elements onto the end of array

http://au.php.net/manual/en/function.array-push.php

Comments

0

You can do:

$array1 = array(array(1,2,3),array(1,2,3));
$array2 = array(array(4,5,6),array(4,5,6));

if(count($array2) > 3){
    foreach($array2 as $val){
            $array1[] = $val;
    }
}
print_r($array1);

or you can simply merge:

$new_array = array_merge($array1 , $array2);

Comments

0

if you still insists in using array push, try this :

if (count($secondArr) < 3) {
    echo "<pre>";
    foreach($firstArr as $arr){
        array_push($secondArr,$arr);
    }
    print_r($secondArr);
    exit;
}

but i agree to use array_merge.

Comments

0

You can try the following code with array_merge

if (count($secondArr) < 3) {
            array_merge($secondArr, $firstArr);
        }

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.