note 100607 deleted from function.array-merge by salathe
| From: | salathe@php.net | Date: | Mon, 06 Sep 2021 22:47:10 +0000 |
| Subject: | note 100607 deleted from function.array-merge by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-216908@lists.php.net to get a copy of this message | ||
Note Submitter: craig ala craigatx.com
----
Reiterating the notes about casting to arrays, be sure to cast if one of the arrays might be null:
<?php
header("Content-type:text/plain");
$a = array('zzzz', 'xxxx');
$b = array('mmmm','nnnn');
echo "1 ==============\r\n";
print_r(array_merge($a, $b));
echo "2 ==============\r\n";
$b = array();
print_r(array_merge($a, $b));
echo "3 ==============\r\n";
$b = null;
print_r(array_merge($a, $b));
echo "4 ==============\r\n";
$b = null;
print_r(array_merge($a, (array)$b));
echo "5 ==============\r\n";
echo is_null(array_merge($a, $b)) ? 'Result is null' : 'Result is not null';
?>
Produces:
1 ==============
Array
(
[0] => zzzz
[1] => xxxx
[2] => mmmm
[3] => nnnn
)
2 ==============
Array
(
[0] => zzzz
[1] => xxxx
)
3 ==============
4 ==============
Array
(
[0] => zzzz
[1] => xxxx
)
5 ==============
Result is null