Hướng dẫn dùng array.splice trong PHP
Định nghĩa hàm array_splice() trong PHPHàm này gỡ bỏ các phần tử đã được chỉ định bởi tham số offset và length từ mảng input, và thay thế chúng bởi các phần tử trong mảng replacement, nếu được cung cấp. Nó trả về một mảng chứa các phần tử đã bị trích (bị gỡ). Show Cú pháp hàm array_splice() trong PHPHàm
Tham số
Trả về giá trị Nó trả về value cuối của mảng, rút gọn mảng đi một phần tử. Ví dụ minh họa cách sử dụng hàm array_splice() trong PHP:
Lưu chương trình trên trong một file có tên là test.php trong htdocs, sau đó mở trình duyệt và gõ địa chỉ http://localhost:8080/test.php sẽ cho kết quả: Xem thêm Hàm trong php Hàm Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức. Cú phápCú pháp: Trong đó:
Các phần tử tron dấu [ ] có thể không truyền vào: Bài viết này được đăng tại [free tuts .net]
Ví dụ
Code $input = range(0,10); array_splice($input, 5, 4, ["25","22"]); echo ""; print_r($input); echo ""; Kết quả Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 25 [6] => 22 [7] => 9 [8] => 10 )
Code $input = range(0,10); array_splice($input, 2, -2); echo ""; print_r($input); echo ""; Kết quả Array ( [0] => 0 [1] => 1 [2] => 9 [3] => 10 )
Code $input = range(0,10); array_splice($input, 5, 0); echo ""; print_r($input); echo ""; Kết quả Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 ) Tham khảo: php.net (PHP 4, PHP 5, PHP 7, PHP 8) array_splice — Remove a portion of the array and replace it with something else array_splice( Note: Numerical keys in Note: If The input array. If If If If If If Tip To remove everything from If If Note: Keys in the If Returns an array consisting of the extracted elements. Example #1 array_splice() examples The above example will output: Example #2 Equivalent statements to various array_splice() examples The following statements are equivalent: StanE ¶ 6 years ago return mrsohailkhan at gmail dot com ¶ 10 years ago
daniele centamore ¶ 13 years ago
function moveUp($input,$index) { royanee at yahoo dot com ¶ 9 years ago
gilberg_vrn ¶ 6 years ago
$result; charette dot s at gmail ¶ 12 years ago
= array('Hey', 'hey', 'my', 'my'); gideon at i6developments dot com ¶ 18 years ago
weikard at gmx dot de ¶ 16 years ago
kbrown at horizon dot sk dot ca ¶ 19 years ago
plintus at smtp dot ru ¶ 19 years ago
news_yodpeirs at thoftware dot de ¶ 12 years ago
csaba at alum dot mit dot edu ¶ 17 years ago
guillaume dot lacourt at gmail dot com ¶ 7 years ago
thom ¶ 8 years ago
jrhardytwothousandtwo at yahoo dot com ¶ 20 years ago
?>
dead dot screamer at seznam dot cz ¶ 13 years ago
$input; pauljamescampbell at gmail dot com ¶ 14 years ago
ahigerd at stratitec dot com ¶ 15 years ago
Paul ¶ 16 years ago
Anonymous ¶ 20 years ago
paule at cs dot tamu dot edu ¶ 20 years ago
paule at cs dot tamu dot edu ¶ 20 years ago
mip at ycn dot com ¶ 15 years ago
= "a";
bdjumakov at gmail dot com ¶ 16 years ago
kokos at lac dot lviv dot ua ¶ 20 years ago
Anonymous ¶ 1 year ago
vitospericolato at gmail dot com ¶ 6 years ago
$array_to_prune as $i=>$value){ Hayley Watson ¶ 4 years ago
randomdestination at gmail dot com ¶ 17 years ago
$out; antrik ¶ 8 years ago
news_yodpeirs at thoftware dot de ¶ 11 years ago
madmax at max-worlds dot net ¶ 13 years ago
Anonymous ¶ 8 years ago
$i = $offset; $i < $length; $i++ ) tsunaquake DOESNTLIKESPAM @ wp DOT pl ¶ 19 years ago
loushou - life dot 42 at gmail dot com ¶ 14 years ago
rolandfoxx at yahoo dot com ¶ 18 years ago
Node { Francis ¶ 14 years ago
strata_ranger at hotmail dot com ¶ 13 years ago
|