Làm cách nào để xóa hai phần tử cuối cùng của một mảng trong PHP?

❮ 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($
?>

Tự mình thử »


Đị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ảng

chi 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 PHP

Bạ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. */
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;
};
0

/* 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;
};
1

/* 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;
};
2

/* 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;
};
3

/* 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;
};
4

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

Làm cách nào để xóa phần tử cuối cùng khỏi một mảng trong PHP?

Hàm array_pop() xóa phần tử cuối cùng của một mảng.

Làm cách nào để xóa hai phần tử đầu tiên khỏi mảng trong PHP?

Để xóa phần tử hoặc giá trị đầu tiên khỏi một mảng, sử dụng hàm array_shift() . Hàm này cũng trả về phần tử bị loại bỏ của mảng và trả về NULL nếu mảng trống.

Làm cách nào để xóa phần tử khỏi mảng trong PHP?

Nếu bạn chỉ muốn xóa một phần tử mảng, bạn có thể sử dụng unset() hoặc \array_splice() . Nếu bạn biết giá trị và không biết khóa để xóa phần tử, bạn có thể sử dụng \array_search() để lấy khóa.

Phương pháp nào được sử dụng để loại bỏ phần tử cuối cùng khỏi một mảng?

Phương thức JavaScript pop() . Phương thức pop() là một phương thức tích hợp để loại bỏ phần tử cuối cùng khỏi một mảng và trả về phần tử đó. Phương pháp này làm giảm độ dài của mảng xuống 1. cú pháp. mảng.