2

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 .

4

5 Answers 5

5

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]
Sign up to request clarification or add additional context in comments.

Comments

0

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

PHP

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

that should work.

Comments

0

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

1 Comment

Yeah Thanks i got it this time
0

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

Comments

-2

$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']));

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.