note 27414 deleted from ref.array by sniper
| From: | sniper@php.net | Date: | Sat, 18 Oct 2003 18:03:29 +0000 |
| Subject: | note 27414 deleted from ref.array by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-58836@lists.php.net to get a copy of this message | ||
Note Submitter: mailman(AT)hit-squad.net
----
I noticed a lack of a function to insert a value into a specified place in an array. Here'a a
function I came up with that will do this:
function array_insert(&$array, $value, $pos)
{
if (!is_array($array))
return FALSE;
$last = array_splice($array, $pos);
array_push($array, $value);
$array = array_merge($array, $last);
}
It can be used like this:
$a = array("zero", "one", "two", "three", "four",
"five", "six", "seven", "eight");
array_insert($a, "POOP", 4);
The array will now contain:
("zero", "one", "two", "three", "POOP",
"four", "five", "six", "seven", "eight")