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?
You can use the PHP array_unique() function and PHP array_merge() function together to merge two arrays into one array without duplicate values in PHP.
Approach 1: Convert object into data array and merge them using array_merge() function and convert this merged array back into object of class stdClass. Note: While merging the objects using array_merge(), elements of array in argument1 are overwritten by elements of array in argument2.
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
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With