note 35556 modified in function.array-merge by vrana

From: Date: Tue, 17 Aug 2004 13:58:56 +0000
Subject: note 35556 modified in function.array-merge by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-80029@lists.php.net to get a copy of this message
I, too have found array_merge_clobber() useful. I use it in a loop to append data to an array. However, when the loop first runs, there will be an error message displayed as array one is empty, so, here is a little fix: <?php function array_merge_clobber($arrOne,$arrTwo) { if(!is_array($arrOne) || !is_array($arrTwo)) return false; $arrNewArray = $arrOne; forEach ($arrTwo as $key => $val) { if (isSet($arrNewArray[$key])) { if (is_array($val) && is_array($arrNewArray[$key])) { $arrNewArray[$key] = array_merge_clobber($arrNewArray[$key], $val); } else { $arrNewArray[$key] = $val; } } else { $arrNewArray[$key] = $val; } } return $arrNewArray; } ?> Not sure if someone else has already got something similar or better, but this is simple and works for me. :) --was-- I, too have found array_merge_clobber() useful. I use it in a loop to append data to an array. However, when the loop first runs, there will be an error message displayed as array one is empty, so, here is a little fix: function array_merge_clobber($arrOne,$arrTwo) { if(!is_array($arrOne) || !is_array($arrTwo)) return false; $arrNewArray = $arrOne; forEach ($arrTwo as $key => $val) { if (isSet($arrNewArray[$key])) { if (is_array($val) && is_array($arrNewArray[$key])) { $arrNewArray[$key] = array_merge_clobber($arrNewArray[$key], $val); } else { $arrNewArray[$key] = $val; } } else { $arrNewArray[$key] = $val; } } return $arrNewArray; } Not sure if someone else has already got something similar or better, but this is simple and works for me. :) http://php.net/manual/en/function.array-merge.php

« previous php.notes (#80029) next »