note 15921 deleted from function.array-merge by irc-html
| From: | irc-html@php.net | Date: | Sat, 19 Jan 2002 21:05:02 +0000 |
| Subject: | note 15921 deleted from function.array-merge by irc-html | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-25465@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 )