note 83808 added to function.array-unshift
| From: | RafaelM dot Salvioni at osu1 dot php dot net | Date: | Thu, 12 Jun 2008 16:52:07 +0000 |
| Subject: | note 83808 added to function.array-unshift | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-140385@lists.php.net to get a copy of this message | ||
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);
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 201.83.141.153
----
Manual Page -- http://www.php.net/manual/en/function.array-unshift.php
Edit -- https://master.php.net/note/edit/83808
Del: integrated -- https://master.php.net/note/delete/83808/integrated
Del: useless -- https://master.php.net/note/delete/83808/useless
Del: bad code -- https://master.php.net/note/delete/83808/bad+code
Del: spam -- https://master.php.net/note/delete/83808/spam
Del: non-english -- https://master.php.net/note/delete/83808/non-english
Del: in docs -- https://master.php.net/note/delete/83808/in+docs
Del: other reasons-- https://master.php.net/note/delete/83808
Reject -- https://master.php.net/note/reject/83808
Search -- https://master.php.net/manage/user-notes.php