Megan Fox
Megan Fox

Reputation: 435

Warning: array_merge(): Argument #2 is not an array Warning: array_merge(): Argument #1 is not an array

Please take a look to my code below .

    $referenceTable = array();
    $referenceTable['val1'] = array(1, 2);
    $referenceTable['val2'] = 3;
    $referenceTable['val3'] = array(4, 5);

    $testArray = array();

    $testArray = array_merge($testArray, $referenceTable['val1']);
    var_dump($testArray);
    $testArray = array_merge($testArray, $referenceTable['val2']);
    var_dump($testArray);
    $testArray = array_merge($testArray, $referenceTable['val3']);
    var_dump($testArray);

I was trying to work with two arrays as you can see and while trying to merge the empty array with the older ones i am getting the warnings as

Warning: array_merge(): Argument #2 is not an array
Warning: array_merge(): Argument #1 is not an array

The Output which i get is

array(2) { [0]=> int(1) [1]=> int(2) }
NULL
NULL

I am unable to fix this thing , help appreciated .

Upvotes: 2

Views: 27018

Answers (5)

AbraCadaver
AbraCadaver

Reputation: 79024

All arguments passed to array_merge() need to be arrays and $referenceTable['val2'] is not an array it is integer 3. You can cast it to an array:

$testArray = array_merge($testArray, (array)$referenceTable['val2']);

Or put it in an array [ ]:

$testArray = array_merge($testArray, [ $referenceTable['val2'] ]);

Or if you're actually defining that variable:

$referenceTable['val2'] = array(3);  // or [3]

Upvotes: 5

Japhet Johnson
Japhet Johnson

Reputation: 11

Ok, I was able to resolve this issue and it occurred when I tried to do composer update. The command did not work and it only corrupted the Service.json file in

/var/www/html/app/storage/meta

So what you need to do is to delete the service.json file and replace it with the new one or you do the composer install command.

The website now runs well on https://www.codemint.net

Upvotes: 0

Dhivya Rajagopal
Dhivya Rajagopal

Reputation: 1

$referenceTable['val2'] is not an array it is integer 3.

You can cast into an array or multiple arrays:

$testArray = array_merge($testArray, array($referenceTable['val2']));

Upvotes: -2

Machavity
Machavity

Reputation: 31654

Breaking this down

$testArray = array_merge($testArray, $referenceTable['val2']);

The problem here is that

$referenceTable['val2'] = 3;

3 is not an array. Set it to an array and it works

$referenceTable['val2'] = array(3);

As to why this fails

$testArray = array_merge($testArray, $referenceTable['val3']);

You ran the previous statement, which set $testArray to NULL, which is also not an array

Upvotes: 0

Sofiene Djebali
Sofiene Djebali

Reputation: 4508

$referenceTable['val2'] is an int not an array, declare $referenceTable like this to be array :

PHP

$referenceTable['val2'] = [3];

that should work.

Upvotes: 0

Related Questions