note 79904 added to language.types.array
| From: | pepesantillan at gmail dot com | Date: | Wed, 19 Dec 2007 00:25:25 +0000 |
| Subject: | note 79904 added to language.types.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-133766@lists.php.net to get a copy of this message | ||
z on 22-Apr-2005 12:10 wrote:
-----------------------------------------------
Here's a simple function to insert a value into some position in an array
<?php
function array_insert($array,$pos,$val)
{
$array2 = array_splice($array,$pos);
$array[] = $val;
$array = array_merge($array,$array2);
return $array;
}
?>
and now for example...
<?php
$a = array("John","Paul","Peter");
$a = array_insert($a,1,"Mike");
?>
Now $a will be "John","Mike","Paul","Peter"
-----------------------------------------------
Im learning to use PHP and reading about array_splice found that
<?php
function array_insert($array,$pos,$val)
{
$array2 = array_splice($array,$pos);
$array[] = $val;
$array = array_merge($array,$array2);
return $array;
}
$a = array("John","Paul","Peter");
$a = array_insert($a,1,"Mike");
print_r($a);
?>
would output the same as
<?php
$b = array("John","Paul","Peter");
array_splice($b,1,0,array("Mike"));
print_r($b);
?>
----
Server IP: 69.147.83.197
Probable Submitter: 190.42.210.137
----
Manual Page -- http://www.php.net/manual/en/language.types.array.php
Edit -- https://master.php.net/note/edit/79904
Del: integrated -- https://master.php.net/note/delete/79904/integrated
Del: useless -- https://master.php.net/note/delete/79904/useless
Del: bad code -- https://master.php.net/note/delete/79904/bad+code
Del: spam -- https://master.php.net/note/delete/79904/spam
Del: non-english -- https://master.php.net/note/delete/79904/non-english
Del: in docs -- https://master.php.net/note/delete/79904/in+docs
Del: other reasons-- https://master.php.net/note/delete/79904
Reject -- https://master.php.net/note/reject/79904
Search -- https://master.php.net/manage/user-notes.php