note 122017 deleted from ref.array by cmb

From: Date: Wed, 13 Dec 2017 12:12:43 +0000
Subject: note 122017 deleted from ref.array by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-209362@lists.php.net to get a copy of this message
Note Submitter: Lawrence DOliveiro ---- *Beware* that there is no proper built-in array-concatenation function. Consider the following two arrays: $arr1 = array(1, 2); $arr2 = array("red", "green", "blue"); If you try print_r($arr1 + $arr2); it produces the output Array ( [0] => 1 [1] => 2 [2] => blue ) which is probably not what you expect. The problem is that PHP tries to combine arrays and dictionaries into a single concept, which doesn’t quite work right as either. Here is a proper array-concatenation function, which can take any number of array arguments: function array_concat(...$arrays) /* concatenates the items of a sequence of arrays. */ { $result = array(); foreach ($arrays as $arr) { foreach ($arr as $elt) { $result[] = $elt; } /*foreach*/ } /*foreach*/ return $result; } /*array_concat*/ The output from print_r(array_concat($arr1, $arr2)); is the much more reasonable Array ( [0] => 1 [1] => 2 [2] => red [3] => green [4] => blue )

« previous php.notes (#209362) next »