PHP array_splice() İşlevi
Örnek
Bir dizideki öğeleri kaldırın ve yeni öğelerle değiştirin:
<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"purple","b"=>"orange");
array_splice($a1,0,2,$a2);
print_r($a1);
?>
Tanım ve Kullanım
array_splice() işlevi, bir diziden seçilen öğeleri kaldırır ve onu yeni öğelerle değiştirir. İşlev ayrıca kaldırılan öğeleri içeren bir dizi döndürür.
İpucu: İşlev herhangi bir öğeyi kaldırmazsa (uzunluk=0), değiştirilen dizi, başlangıç parametresinin konumundan eklenir (Bkz. Örnek 2).
Not: Değiştirilen dizideki anahtarlar korunmaz.
Sözdizimi
array_splice(array, start, length, array)
Parametre Değerleri
Parameter | Description |
---|---|
array | Required. Specifies an array |
start | Required. Numeric value. Specifies where the function will start
removing elements.
0 = the first element.
If this value is set to a negative number, the function will start that far from the last element.
-2 means start at the second last element of the array. |
length | Optional. Numeric value. Specifies how many elements will
be removed, and also length of the returned array.
If this value is set to a negative number, the function will stop that far from the last element. If this value is not set, the function will
remove all elements, starting from the position set by the start-parameter.
|
array | Optional. Specifies an array with the elements that will be inserted to the original array. If it's only one element, it can be a string, and does not have to be an array. |
Teknik detaylar
Geri dönüş değeri: | Ayıklanan öğelerden oluşan diziyi döndürür |
---|---|
PHP Sürümü: | 4+ |
Daha fazla örnek
örnek 1
Sayfanın üstündeki örnekle aynı örnek, ancak çıktı döndürülen dizidir:
<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"purple","b"=>"orange");
print_r(array_splice($a1,0,2,$a2));
?>
Örnek 2
Uzunluk parametresi 0 olarak ayarlandığında:
<?php
$a1=array("0"=>"red","1"=>"green");
$a2=array("0"=>"purple","1"=>"orange");
array_splice($a1,1,0,$a2);
print_r($a1);
?>
❮ PHP Dizi Referansı