note 15921 added to function.array-merge
| From: | the_php_guy at e-clecticity dot net | Date: | Mon, 08 Oct 2001 21:58:41 +0000 |
| Subject: | note 15921 added to function.array-merge | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-18701@lists.php.net to get a copy of this message | ||
Here's another handy use for array_merge:
Let's say you have an array whose numeric index has gaps in it (for example, elements have been
deleted). If you were to merge it with another array, those gaps would disappear. But suppose you
just want to get rid of the gaps and not add any elements. Simple. Just pass NULL (which can be
substituted for any type) as the second argument. Your array is merged with nothing and then the
index is renumbered.
For example, the following code:
<?php
$a = array(1 => "Apples", 3 => "Oranges", 56 => "Pears");
echo "Before using array_merge, array = ";
print_r($a);
$b = array_merge($a, NULL);
echo "After using array_merge, array = ";
print_r($b);
?>
results in this output:
Before using array_merge, array = Array ( [1] => Apples [3] => Oranges [56] => Pears )
After using array_merge, array = Array ( [0] => Apples [1] => Oranges [2] => Pears )
--
http://www.php.net/manual/en/function.array-merge.php
http://master.php.net/manage/user-notes.php?action=edit+15921
http://master.php.net/manage/user-notes.php?action=delete+15921
http://master.php.net/manage/user-notes.php?action=reject+15921