Xóa phần tử đầu tiên của mảng kết hợp PHP
Cho một mảng (Một chiều hoặc nhiều chiều) và nhiệm vụ là xóa một phần tử của mảng dựa trên giá trị khóa Show
ví dụ Input: Array ( [0] => 'G' [1] => 'E' [2] => 'E' [3] => 'K' [4] => 'S' ) Key = 2 Output: Array ( [0] => 'G' [1] => 'E' [3] => 'K' [4] => 'S' ) Khuyến khích. Vui lòng thử cách tiếp cận của bạn trên {IDE} trước, trước khi chuyển sang giải phápSử 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. Sau khi xóa khóa và giá trị được liên kết không thay đổi cú pháp unset($variable) Tham số. Hàm này chấp nhận biến tham số duy nhất. Nó là tham số bắt buộc và được sử dụng để bỏ đặt phần tử chương trình 1. Xóa phần tử khỏi mảng một chiều unset($variable)7 unset($variable)8 unset($variable)9 unset($variable)0 unset($variable)1 unset($variable)2 unset($variable)0 unset($variable)1 unset($variable)2_______13 unset($variable)4 unset($variable)5 unset($variable)6 unset($variable)5 unset($variable)6 unset($variable)5 unset($variable)20 unset($variable)5 unset($variable)22 unset($variable)23 unset($variable)0 unset($variable)25 unset($variable)26 unset($variable)0____123 unset($variable)0 unset($variable)00 unset($variable)01 unset($variable)02 unset($variable)0 unset($variable)04 unset($variable)0 unset($variable)25 unset($variable)26 unset($variable)0____123 unset($variable)0 unset($variable)71 đầu ra. unset($variable)2 chương trình 2. Xóa một phần tử khỏi mảng kết hợp unset($variable)72 unset($variable)0 unset($variable)74 unset($variable)75 unset($variable)76 unset($variable)1____12 unset($variable)79 unset($variable)80 _______581____582 unset($variable)81 unset($variable)84 unset($variable)85____12 unset($variable)79 unset($variable)88 unset($variable)89 unset($variable)90 unset($variable)89 unset($variable)92 unset($variable)89 unset($variable)94 unset($variable)95 unset($variable)89 unset($variable)97 unset($variable)98 unset($variable)81 unset($variable)00 unset($variable)88 unset($variable)81 unset($variable)03 unset($variable)81 unset($variable)05 unset($variable)85____12 unset($variable)79 unset($variable)88 unset($variable)89 unset($variable)90 unset($variable)89 unset($variable)92 unset($variable)89 unset($variable)94 unset($variable)16 unset($variable)89 unset($variable)97 unset($variable)19 unset($variable)81 unset($variable)00 unset($variable)80 unset($variable)81 unset($variable)24 unset($variable)81 unset($variable)26 unset($variable)85____12 unset($variable)79 unset($variable)88 unset($variable)89 unset($variable)90 unset($variable)89 unset($variable)92 unset($variable)89 unset($variable)94 unset($variable)07 unset($variable)89 unset($variable)97 unset($variable)10 unset($variable)81 unset($variable)00 unset($variable)13 unset($variable)0 unset($variable)15 unset($variable)16 unset($variable)17 unset($variable)0 unset($variable)19 unset($variable)26 unset($variable)76 unset($variable)13 unset($variable)0 unset($variable)24 unset($variable)25 unset($variable)02 unset($variable)76____128 unset($variable)05 unset($variable)30 unset($variable)0 unset($variable)15 unset($variable)33 unset($variable)17 unset($variable)0 unset($variable)19 unset($variable)26 unset($variable)76 unset($variable)13 unset($variable)0 unset($variable)41 đầu ra. unset($variable)0 PHP là ngôn ngữ kịch bản phía máy chủ được thiết kế dành riêng cho phát triển web. Bạn có thể học PHP từ đầu bằng cách làm theo Hướng dẫn PHP và Ví dụ về PHP này Bạn có thể xóa các phần tử khỏi một mảng kết hợp không?Nếu muốn xóa một phần tử khỏi mảng, bạn chỉ cần dùng hàm unset() . Ví dụ sau đây cho thấy cách xóa một phần tử khỏi mảng kết hợp và mảng số.
Làm cách nào để xóa khóa 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.
Làm cách nào để xóa một phần tử cụ thể khỏi một mảng trong PHP?Hàm 'unset' có thể được sử dụng để xóa một phần tử khỏi mảng và sử dụng hàm 'array_values' để đặt lại các chỉ số của mảng.
Làm cách nào để cắt mảng kết hợp trong PHP?Để xóa một khóa và giá trị tương ứng của nó khỏi một mảng kết hợp trong PHP, bạn có thể sử dụng hàm unset() . Như tên của hàm gợi ý, bạn sử dụng hàm unset() để bỏ đặt một biến đã cho hoặc trong trường hợp này là một khóa mảng với giá trị của nó. |