note 83808 deleted from function.array-unshift by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 08:02:09 +0000 |
| Subject: | note 83808 deleted from function.array-unshift by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-196748@lists.php.net to get a copy of this message | ||
Note Submitter: Rafael M. Salvioni
----
This function inserts a element in any position of the Array, by reference.
NOTE: The array is converted for a numeric array.
<?php
/**
* Function array_insert().
*
* Returns the new number of the elements in the array.
*
* @param array $array Array (by reference)
* @param mixed $value New element
* @param int $offset Position
* @return int
*/
function array_insert(&$array, $value, $offset)
{
if (is_array($array)) {
$array = array_values($array);
$offset = intval($offset);
if ($offset < 0 || $offset >= count($array)) {
array_push($array, $value);
} elseif ($offset == 0) {
array_unshift($array, $value);
} else {
$temp = array_slice($array, 0, $offset);
array_push($temp, $value);
$array = array_slice($array, $offset);
$array = array_merge($temp, $array);
}
} else {
$array = array($value);
}
return count($array);
}
?>