17

Trying to merge 4 arrays, but some may be empty at certain times.

$array_1 = array('something1', something2);
$array_2 = array('something3', something4);
$array_3 = array();
$array_4 = array('something1', something2);

$list = array_merge($array_1,$array_2,$array_3,$array_4);

print_r($list);

But if one of the arrays are empty, there will be an error. I've been googling forever, but I can't find a solid simple answer on how to check for empty arrays before merging.

Argument #2 is not an array

Or whichever array is empty is the argument number. How do I strip out empty arrays before merging?

6
  • if you make array of arrays of them, you can loop through it, perform multiple merges and do the empty() checks there Commented Nov 21, 2013 at 21:13
  • Would you have an example? Commented Nov 21, 2013 at 21:13
  • okay i made an answer, may or may not help Commented Nov 21, 2013 at 21:20
  • 3
    There is NO error with an empty array. There is only an error if the arg is NOT an array. Commented Nov 21, 2013 at 21:23
  • merging an empty array is NOT an error. Commented Nov 21, 2013 at 21:28

3 Answers 3

19

There is NO error with an empty array. There is only an error if the arg is NOT an array.

You could check is_array() or:

$list = array_merge(
(array)$array_1,
(array)$array_2,
(array)$array_3,
(array)$array_4
);
Sign up to request clarification or add additional context in comments.

Comments

15

Okay here you go, this should do the trick (if you make array of the initial arrays):

$arrs = array();

$arrs[] = array('something1', something2);
$arrs[] = array('something3', something4);
$arrs[] = array();
$arrs[] = array('something1', something2);

$list = array();

foreach($arrs as $arr) {
    if(is_array($arr)) {
        $list = array_merge($list, $arr);
    }
}

print_r($list);

5 Comments

This worked perfect, never thought of making the variables arrays and looping through. Thanks!
isn't $list empty on the first iteration?
@Yada tbh I dont think array_merge has a problem with empty array, I suspect David tried to feed null or undefined variable into it.
if(!empty($arr)) should be if(is_array($arr)). As others have pointed out, you need to ensure that it is an array, it doesn't matter if it is empty.
Yes really good but you should avoid empty array as well, if (is_array($arr)&&!empty($arr)) { $list = array_merge($list, $arr); } , got problem in json_encode. :)
5

Array merge supports empty array()

Doc:

Example #3 Simple array_merge() example https://www.php.net/array_merge

<?php
$array1 = array();
$array2 = array(1 => "data");
$result = array_merge($array1, $array2);
?>

Result
Array
(
    [0] => data
)

You are getting notice because something2, something4 should be quoted as string or $ as variable.

PHP Notice:  Use of undefined constant something2 - assumed 'something2' 

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.