note 100607 added to function.array-merge

From: Date: Tue, 26 Oct 2010 17:25:34 +0000
Subject: note 100607 added to function.array-merge
Groups: php.notes 
Request: Send a blank email to php-notes+get-173475@lists.php.net to get a copy of this message
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 ---- Server IP: 208.69.120.55 Probable Submitter: 64.132.0.250 ---- Manual Page -- http://www.php.net/manual/en/function.array-merge.php Edit -- https://master.php.net/note/edit/100607 Del: integrated -- https://master.php.net/note/delete/100607/integrated Del: useless -- https://master.php.net/note/delete/100607/useless Del: bad code -- https://master.php.net/note/delete/100607/bad+code Del: spam -- https://master.php.net/note/delete/100607/spam Del: non-english -- https://master.php.net/note/delete/100607/non-english Del: in docs -- https://master.php.net/note/delete/100607/in+docs Del: other reasons-- https://master.php.net/note/delete/100607 Reject -- https://master.php.net/note/reject/100607 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#173475) next »