I'm trying to insert a value into the middle of a simple
(numerically-ordered) array, and bump all the later array keys up one.
AFAIK, there isn't a function to do this already, so this is the code I
came up with... is this the best way of doing it?
<?php
function array_insert($array, $value, $position)
{
if (is_array($array)) {
$array_out = $array; // so I don't mangle it during foreach
foreach ($array as $key => $val) {
if ($key < $val) { $array_out[$key] = $val; }
else { $array_out[$key+1] = $val; }
}
$array_out[$position] = $value;
return $array_out;
}
return false;
}
?>
--
-- Rudy Fleminger
-- sp@mmers.and.evil.ones.will.bow-down-to.us
(put "Hey!" in the Subject line for priority processing!)
-- http://www.pixelsaredead.com