Hàm array_splice[] loại bỏ các phần tử đã chọn khỏi một mảng và thay thế nó bằng các phần tử mới. Hàm cũng trả về một mảng với các phần tử đã bị xóa
Mẹo. Nếu hàm không loại bỏ bất kỳ phần tử nào [độ dài = 0] thì mảng được thay thế sẽ được chèn vào từ vị trí của tham số bắt đầu [Xem ví dụ 2]
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.
________số 8_______
$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:
Array
[
[c] => 3
[d] => 4
[e] => 5
]
Array
[
[a] => 1
[b] => 2
[0] => bar
[1] => baz
[f] => 6
]
But with array_splice_assoc[]
$input = array['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' =>6];
print_r[array_splice_assoc[$input, -4, 3, array['foo1' => 'bar', 'foo2' => 'baz']]];
print_r[$input];
we get:
/* 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;
};
Đôi khi trong quá trình làm việc với PHP, cần xóa và lập chỉ mục lại một phần tử mảng. Ở đây, chúng tôi đã tập hợp các phương pháp hữu ích sẽ giúp đạt được điều đó
Kiến thức về CSS
Trong phần này, chúng ta sẽ tiến hành xóa một phần tử mảng bằng hàm unset[] và sau đó lập chỉ mục lại nó bằng array_values[]
Hàm unset[] được sử dụng để hủy đặt một biến đã cho
Cú pháp như sau
unset[mixed $var, mixed ...$vars]: void
Hàm array_values[] nhằm mục đích trả về tất cả các giá trị từ mảng và lập chỉ mục bằng số cho mảng
Cú pháp như sau
array array_values [ array $array ]
Bây giờ, hãy xem hai hàm này hoạt động như thế nào
Đầu ra sẽ hiển thị như sau
array[2] { [0]=> string[5] "w3docs" [2]=> string[5] "w3docs" } array[2] { [0]=> string[5] "w3docs" [1]=> string[5] "w3docs" }
Có một giải pháp thay thế, quá. Nó đang sử dụng hàm array_splice[], được sử dụng để loại bỏ một phần của mảng và thay thế nó bằng bất kỳ thứ gì khác
Cú pháp của hàm này như sau
array_splice [ array &$input , int $offset [, int $length = count[$input] [, mixed $replacement = array[] ]] ] : array
Để hiểu rõ hơn về cách chức năng này hoạt động, hãy xem ví dụ bên dưới
Đầu ra sẽ như sau
array[2] { [0]=> string[5] "w3docs" [1]=> string[5] "w3docs" }
Mảng là một biến chứa tập hợp các giá trị khác, có sẵn trên các chỉ số cụ thể. Để truy cập giá trị của một mảng, chúng ta cần chỉ định tên của mảng và chỉ số của dữ liệu