Xóa khỏi danh sách php

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:

Array
(
    [c] => 3
    [d] => 4
    [e] => 5
)
Array
(
    [a] => 1
    [b] => 2
    [0] => bar
    [1] => baz
    [f] => 6
)

But with array_splice_assoc()

________số 8

we get:

/* 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


Xóa mục được chỉ định

Phương thức /* 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;
};
5 xóa mục đã chỉ định

Thí dụ

Loại bỏ "chuối"

thislist = ["apple", "banana", "cherry"]
thislist. remove("banana")
print(thislist)

Tự mình thử »


Xóa chỉ mục đã chỉ định

Phương thức /* 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;
};
6 xóa chỉ mục đã chỉ định

Thí dụ

Xóa mục thứ hai

thislist = ["apple", "banana", "cherry"]
thislist. pop(1)
print(thislist)

Tự mình thử »

Nếu bạn không chỉ định chỉ mục, phương thức /* 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;
};
6 sẽ xóa mục cuối cùng

Thí dụ

Xóa mục cuối cùng

thislist = ["apple", "banana", "cherry"]
thislist. pop()
print(thislist)

Tự mình thử »

Từ khóa /* 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;
};
8 cũng xóa chỉ mục đã chỉ định

Thí dụ

Xóa mục đầu tiên

thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)

Tự mình thử »

Từ khóa /* 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;
};
8 cũng có thể xóa hoàn toàn danh sách

Thí dụ

Xóa toàn bộ danh sách

thislist = ["apple", "banana", "cherry"]
del thislist

Tự mình thử »


Xóa danh sách

Phương thức Apart from preserving the keys, it behaves just like the regular array_splice() for all cases I could think of.0 làm trống danh sách

Danh sách vẫn còn, nhưng nó không có nội dung

Thí dụ

Xóa nội dung danh sách

thislist = ["apple", "banana", "cherry"]
thislist. clear()
print(thislist)

Tự mình thử »




Loại bỏ các phần tử mảng

Bạn có thể sử dụng phương thức /* 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;
};
6 để xóa một phần tử khỏi mảng

Bạn cũng có thể sử dụng phương thức /* 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;
};
5 để xóa một phần tử khỏi mảng

Ghi chú. Phương thức /* 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;
};
5 của danh sách chỉ xóa lần xuất hiện đầu tiên của giá trị đã chỉ định




Làm cách nào để xóa một mục khỏi danh sách trong PHP?

Để xóa phần tử khỏi mảng, chúng ta có thể sử dụng hàm unset() để xóa phần tử khỏi mảng và sau đó sử dụng hàm array_values() lập chỉ mục cho mảng bằng số . .

Làm cách nào để xóa một mục cụ thể khỏi một mảng trong PHP?

Sử dụng hàm unset() . Hàm unset() dùng để xóa phần tử khỏi mảng. Hàm unset được sử dụng để hủy bất kỳ biến nào khác và sử dụng tương tự để xóa bất kỳ phần tử nào của một mảng. Lệnh bỏ đặt này lấy khóa mảng làm đầu vào và xóa phần tử đó khỏi mảng.

Làm cách nào để xóa chuỗi khỏi mảng trong PHP?

Trả lời. Sử dụng Hàm unset() của PHP .

Làm cách nào để xóa giá trị khỏi mảng kết hợp trong PHP?

Phương pháp 1. Sử dụng hàm unset() . Hàm unset() được sử dụng để bỏ đặt khóa và giá trị của nó trong một mảng kết hợp. print_r( $arr ); . Sử dụng hàm array_diff_key(). Hàm này được sử dụng để lấy sự khác biệt giữa một hoặc nhiều mảng.