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?

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

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

đầu ra

array["a" => "Apple", "c" => "Cat"]

Một vi dụ khac

1

2

3

4

5

đầ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?

Hàm JavaScript pop[] . Phương thức này dùng để loại bỏ các phần tử ở cuối mảng. Hàm shift[] trong JavaScript. Phương thức này được sử dụng để loại bỏ các phần tử từ đầu của một mảng. Hàm nối JavaScript []. Phương pháp này được sử dụng để xóa các phần tử khỏi chỉ mục cụ thể của một mảng.

Làm cách nào để xóa dữ liệu cụ thể khỏi mảng trong PHP?

Trả lời. Sử dụng Hàm unset[] của 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?

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[].

Làm cách nào để thêm bớt phần tử mảng trong PHP?

php // Thêm phần tử vào đầu mảng array_unshift[$array, 'zero']; Đây là cách xóa một phần tử khỏi .

Chủ Đề