|
|
 |
(PHP 4 >= 4.0.0) array_splice -- Odstranit část pole a nahradit ji něčím jiným Popisarray array_splice ( array input, int offset [, int length [, array replacement]])
array_splice() odstraňuje prvky pole
input určené argumenty offset
a length, a případně je nahrazuje prvky volitelného
argumentu (pole) replacement.
Pokud je offset kladný, tato odstraněná část začne
offset položek od začátku
array. Pokud je offset
záporný, začne tolik položek od konce array.
Pokud vynecháte length,
array_splice() odstraní všechno od
offset do konce pole. Pokud je
length kladná, odstraní se právě tolik prvků. Pokud
je length záporná, konec odstraněné části bude
právě tolik prvků od konce pole. Tip: k odstranění všech prvků od
offset do konce pole při současně určeném argumentu
replacement použijte jako
length count($input).
Pokud zadáte replacement pole, odstraněné prvky se
nahradí prvky tohoto pole. Pokud argumenty offset a
length definovány tak, že se nic neodstraní, prvky
pole replacement se vloží na místo určené argumentem
offset. Tip: pokud je replacement
jen jedna hodnota, není nutno ji umisťovat do array(),
ledaže chcete, aby tato položka byla opravdu pole.
Následující volání jsou ekvivalentní:
array_push ($input, $x, $y) array_splice ($input, count ($input), 0,
array ($x, $y))
array_pop ($input) array_splice ($input, -1)
array_shift ($input) array_splice ($input, 0, 1)
array_unshift ($input, $x, $y) array_splice ($input, 0, 0, array ($x, $y))
$a[$x] = $y array_splice ($input, $x, 1, $y) |
Vrací pole odstraněných prvků.
Příklad 1. Ukázky array_splice() $input = array ("red", "green", "blue", "yellow");
array_splice ($input, 2); // $input is now array ("red", "green")
array_splice ($input, 1, -1); // $input is now array ("red", "yellow")
array_splice ($input, 1, count($input), "orange");
// $input is now array ("red", "orange")
array_splice ($input, -1, 1, array("black", "maroon"));
// $input is now array ("red", "green",
// "blue", "black", "maroon") |
|
Viz také: array_slice().
User Contributed Notes array_splice |
 |
andrei@ispi.net
29-Apr-2000 01:24 |
|
Here goes a solution to the problem of being unable to remove an element by
key.
$key_index = array_keys(array_keys($array), $target_key);
array_splice($array, $key_index[0], 1);
|
|
info@borderblue.com
23-Oct-2000 12:16 |
|
note that unset() works well for associative arrays, but if you have
$blah = array("a","b","c","d");
and you unset($blah[2]), then the other
elements will not shift, i.e., you'll still have $blah[0], $blah[1], and
$blah[3]. If you need things to shift, use
array_splice($blah,2,1);
|
|
brissaille@yahoo.com
21-Dec-2000 05:31 |
|
Pay attention, if the keys of your
array are integer, array_splice will
modify them.
For example:
$t=array('5'=>'Albator', '7'=>'Goldorak', 21=>'Candy');
array_splice($t,0,0) gives you the follwing result:
$t=array {[0]=>'Albator', [1]=>'Goldorak', [2]=>'Candy'}
|
|
stf@c-gix.com
19-Dec-2001 02:49 |
|
array_splice always drops keys from the replacement.
Example :
$tmp = array( "a" => "toto" , "b" =>
"titi" , "c" => "tutu" );
array_splice($tmp, 1, 1, array("d" => "tata" ));
print_r($tmp);
will result in :
Array ( [a] => toto [0] => tata [c] => tutu )
where one would expect :
Array ( [a] => toto [d] => tata [c] => tutu )
|
|
leingang AT math DOT rutgers DOT edu
16-Jan-2002 01:32 |
|
array_splice resets the internal pointer of $input. In fact, many array
functions do this. Caveat programmor!
|
|
steve@hynding.com
17-Jan-2002 08:09 |
|
Values removed from the array are returned as an array as such:
$letters = array("a","b","c");
$removed_val = array_splice($letters, 1, 1);
print_r($removed_val);
Printed Result:
Array ( [0] => b )
|
|
jrhardytwothousandtwo@yahoo.com
15-Feb-2002 05:54 |
|
A reference is made to INSERT'ing into an array here with array_splice,
however its not explained very well. I hope this example will help others
find what took me days to research.
--code begin--
$original_array = array(1,2,3,4,5);
$insert_into_key_position = 3;
$item_to_insert = "blue";
$returned = array_splice($original_array, $insert_into_key_position, 0,
$item_to_insert);
$original_array will now show:
1,2,3,blue,4,5
--code end---
Remember that you are telling the array to insert the element into the KEY
position. Thus the elements start with key 0 and so on 0=>1, 1=>2,
2=>3, 3=>blue, 4=>4, 5=>5. And walla, you've inserted. I
can't say if this is of any value for named keys, or multidimensional
arrays. However it does work for single dimensional arrays.
$returned should be an empty array as nothing was returned. This would
have substance if you were doing a replace instead.
|
|
amtiee@hotmail.com
29-Apr-2002 09:35 |
|
array_splice() sometimes seems to be worth nothing, since it asks for the
offset of a key from where to start splicing ...
Here is a fuction that dosent need you to speciy array offset.
function my_array_splice($aArray,$szKey, $iLength)
{
$aKeys = array_keys($aArray);
$iIndex = array_search($szKey, $aKeys);
array_splice($aArray, $iIndex, 1);
}
Call it passing a refernce of array to be spliced ...
my_array_splice(&$aToSpliceArray, $szSpliceFrom, $iSpliceTill);
The above function can be modified/generalized completely to take all the
parameters accepted by array_splice().
|
|
amtiee@hotmail.com
29-Apr-2002 12:36 |
|
The function my_array_splice(), accepts a user defined key of an array.
$aArray ={'Name'=>'ABC','Age'=>'22', 'Sex'=>'M'};
my_array_splice($aArray, 'Age', 1);
So now $aArray will contain only:
['Name'=>'ABC', 'Sex'=>'M']
my_array_splice() internally makes use of array_search(), which is not
supported with PHP <= 4.0.4. A solution to this is to sneak into user
contributed notes of function array_search(). An alternative submitted by
chen.avinadav@vbulletin.com is the solution for this problem.
|
|
brettbeeson@hotmail.com
02-May-2002 03:10 |
|
Further to andrei's note on removing an element. Previous code assumes
element exists. Instead, try :
// Removes an element with $key in $array, if it exists.
function array_remove($key,&$array) {
if (key_exists($key,$array)) {
$key_index = array_keys(array_keys($array), $key);
array_splice($array, $key_index[0], 1);
}
}
|
|
paule@cs.tamu.edu
26-May-2002 01:02 |
|
I believe the following is a version of array_slice that solves most of the
issues for people that want an associative key offset, rather than an
integer.
function key_array_splice(&$input, $key_ofs, $length=NULL,
$replacement=NULL)
{
// Adjust the length if it was negative or not passed
if($length===NULL || $length<0)
$count = $length+count($input);
// Cycle through the array
foreach($input as $key=>$value){
if(!$key_found){
if($key===$key_ofs){
$key_found=true;
if($length!==NULL && $length>=0)
$count=$length;
if(is_array($replacement))
foreach($replacement as $r_key=>$r_value)
$new_array[$r_key]=$r_value;
}else
$new_array[$key]=$value;
}
if($key_found){
if($count>0)
$ret_array[$key]=$value;
else
$new_array[$key]=$value;
}
$count--;
}
// Finish up
$input=$new_array;
return $ret_array;
}
Note that this code needs PHP 4 for the use of the "===" and
"!==" operators.
|
|
 |
 | |