Nội dung chính
Xem Hàm nào được sử dụng để loại bỏ hoặc xóa các phần tử khỏi một mảng trong PHP? 2024
Prompted by dire need, and inspired by some of the existing notes, I came up with this:
/* Like array_splice(), but preserves the key(s) of the replacement array. */
function array_splice_assoc(&$input, $offset, $length = 0, $replacement = array()) {
$tail = array_splice($input, $offset);
$extracted = array_splice($tail, 0, $length);
$input += $replacement + $tail;
return $extracted;
};
Apart from preserving the keys, it behaves just like the regular array_splice() for all cases I could think of.
So for example the regular array_splice()
$input = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' =>6);
print_r(array_splice($input, -4, 3, array('foo1' => 'bar', 'foo2' => 'baz')));
print_r($input);
will give:
________số 8
But with array_splice_assoc()
Array ( [0] => 1 [1] => 2 [2] => 3 [4] => 5 )
0
Array ( [0] => 1 [1] => 2 [2] => 3 [4] => 5 )
1
/* Like array_splice(), but preserves the key(s) of the replacement array. */
0
function array_splice_assoc(&$input, $offset, $length = 0, $replacement = array()) {
$tail = array_splice($input, $offset);
$extracted = array_splice($tail, 0, $length);
$input += $replacement + $tail;
return $extracted;
};
/* Like array_splice(), but preserves the key(s) of the replacement array. */
1
function array_splice_assoc(&$input, $offset, $length = 0, $replacement = array()) {
$tail = array_splice($input, $offset);
$extracted = array_splice($tail, 0, $length);
$input += $replacement + $tail;
return $extracted;
};
/* Like array_splice(), but preserves the key(s) of the replacement array. */
2
function array_splice_assoc(&$input, $offset, $length = 0, $replacement = array()) {
$tail = array_splice($input, $offset);
$extracted = array_splice($tail, 0, $length);
$input += $replacement + $tail;
return $extracted;
};
/* Like array_splice(), but preserves the key(s) of the replacement array. */
3
function array_splice_assoc(&$input, $offset, $length = 0, $replacement = array()) {
$tail = array_splice($input, $offset);
$extracted = array_splice($tail, 0, $length);
$input += $replacement + $tail;
return $extracted;
};
/* Like array_splice(), but preserves the key(s) of the replacement array. */
4
function array_splice_assoc(&$input, $offset, $length = 0, $replacement = array()) {
$tail = array_splice($input, $offset);
$extracted = array_splice($tail, 0, $length);
$input += $replacement + $tail;
return $extracted;
};
Trong hướng dẫn này, bạn sẽ học hai phương thức PHP unset() và array_splice() để loại bỏ các phần tử mảng cụ thể
Sử dụng hàm PHP unset()
Sử dụng hàm PHP unset() để xóa một phần tử khỏi mảng
Về cơ bản, nó được sử dụng để bỏ đặt bất kỳ biến nào trong PHP. Hàm này chấp nhận biến làm đối số và bỏ đặt nó
Ví dụ
1
2
3
4
$arr = mảng(“a” => “apple”, “b” => “ball”, “c” => “cat”);
bỏ đặt($arr[“b”]);
?>
đầu ra
array("a" => "Apple", "c" => "Cat")
Một vi dụ khac
1
2
3
4
5
$arr = mảng(1, 2, 3, 4, 5);
bỏ đặt($arr[3]);
print_r($arr)
?>
đầu ra
Array ( [0] => 1 [1] => 2 [2] => 3 [4] => 5 )
Bạn có thể thấy mảng kết quả không được lập chỉ mục. Để khắc phục tình trạng này, bạn có thể sử dụng hàm array_splice() PHP được mô tả bên dưới
Sử dụng Hàm array_splice() trong PHP
Bạn cũng có thể sử dụng hàm array_splice() để xóa phần tử mảng cụ thể. Hàm này cũng có thể được sử dụng để thay thế các phần tử mảng bằng cách truyền mảng với các giá trị cập nhật
Hàm này nhận ba tham số, một mảng, độ lệch (bắt đầu từ đâu) và độ dài (số phần tử cần xóa)
Hàm nào dùng để xóa phần tử trong mảng?
Làm cách nào để xóa dữ liệu cụ thể khỏi mảng trong PHP?
.
Ví dụ về chức năng nào được sử dụng để xóa một phần tử khỏi mảng?
Làm cách nào để thêm bớt phần tử mảng trong PHP?
Bạn đang tìm hiểu bài viết: Hàm nào được sử dụng để loại bỏ hoặc xóa các phần tử khỏi một mảng trong PHP? 2024
HỆ THỐNG CỬA HÀNG TRÙM SỈ QUẢNG CHÂU
Điện thoại: 092.484.9483
Zalo: 092.484.9483
Facebook: https://facebook.com/giatlathuhuongcom/
Website: Trumsiquangchau.com
Địa chỉ: Ngõ 346 Nam Dư, Trần Phú, Hoàng Mai, Hà Nội.