❮ Tham chiếu mảng PHP
Ví dụ
Xóa phần tử cuối cùng của mảng
$a=array["red","green","blue"];
array_pop[$a];
print_r[$
?>
Định nghĩa và cách sử dụng
Hàm array_pop[] xóa phần tử cuối cùng của mảng
cú pháp
Giá trị tham số
Tham sốMảng mô tảBắt buộc. Chỉ định một mảngchi tiết kỹ thuật
Giá trị trả về. Trả về giá trị cuối cùng của mảng. Nếu mảng trống hoặc không phải là mảng, NULL sẽ được trả về. Phiên bản PHP. 4+❮ Tham chiếu mảng PHPBạn có thể sử dụng hàm PHP array_pop[] xóa phần tử cuối cùng khỏi một mảng trong PHP. Hàm array_pop[] trả về giá trị cuối cùng của mảng. Nếu mảng trống hoặc không phải là mảng thì sẽ trả về NULL
đọc thêm. Cách xóa phần tử đầu tiên khỏi mảng trong PHP
Trước tiên hãy xem đầu ra của mảng $stack
đầu ra
Array [ [0] => yellow [1] => red [2] => green [3] => orange [4] => purple ]
Mảng $stack có 5 phần tử và chúng ta muốn loại bỏ phần tử cuối cùng có giá trị “purple”
Xóa phần tử cuối cùng khỏi một mảng
Bây giờ chúng ta sẽ sử dụng hàm array_pop[] của PHP để xóa phần tử cuối cùng của một mảng như trong ví dụ dưới đây
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] => yellow [1] => red [2] => green [3] => orange [4] => purple ]0
Array [ [0] => yellow [1] => red [2] => green [3] => orange [4] => purple ]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;
};
Array [ [0] => yellow [1] => red [2] => green [3] => orange [4] => purple ]7
Array [ [0] => yellow [1] => red [2] => green [3] => orange [4] => purple ]8
Array [ [0] => yellow [1] => red [2] => green [3] => orange [4] => purple ]9