ISSET [] sẽ trả về: -________ 2 -________ 3 -________ 4
-true if the key exists and the value is != NULL
-false if the key exists and value == NULL
-false if the key does not exist
Array_Key_Exists [] sẽ trả về: -________ 5 -________ 4
-true if the key exists
-false if the key does not exist
Vì vậy, nếu giá trị của bạn có thể là vô giá trị, cách thích hợp là array_key_exists
. Nếu ứng dụng của bạn không phân biệt giữa NULL và không có khóa, sẽ hoạt động, nhưng array_key_exists
luôn cung cấp nhiều tùy chọn hơn.
Trong ví dụ sau, không có khóa nào trong mảng trả về null, nhưng giá trị null cũng vậy cho một khóa nhất định. Điều đó có nghĩa là nó thực sự giống như isset
.
Nhà điều hành Null Coalesce [??] không được thêm vào cho đến Php 7, nhưng điều này hoạt động trở lại Php 5, có thể là 4:
$value = [array_key_exists[$key_to_check, $things] ? $things[$key_to_check] : NULL];
như một chức năng:
function get_from_array[$key_to_check, $things]
return [array_key_exists[$key_to_check,$things] ? $things[$key_to_check] : NULL];
[Php 4> = 4.0.7, Php 5, Php 7, Php 8]
Array_Key_Exists - Kiểm tra xem phím hoặc chỉ mục đã cho có tồn tại trong mảng không — Checks if the given key or index exists in the array
Sự mô tả
Array_Key_Exists [Chuỗi | int
0, Array function get_from_array[$key_to_check, $things]
return [array_key_exists[$key_to_check,$things] ? $things[$key_to_check] : NULL];
1]: Bool[string|int function get_from_array[$key_to_check, $things]
return [array_key_exists[$key_to_check,$things] ? $things[$key_to_check] : NULL];
0, array function get_from_array[$key_to_check, $things]
return [array_key_exists[$key_to_check,$things] ? $things[$key_to_check] : NULL];
1]: boolfunction get_from_array[$key_to_check, $things]
return [array_key_exists[$key_to_check,$things] ? $things[$key_to_check] : NULL];
Thông số
function get_from_array[$key_to_check, $things]
return [array_key_exists[$key_to_check,$things] ? $things[$key_to_check] : NULL];
2Giá trị để kiểm tra.
function get_from_array[$key_to_check, $things]
return [array_key_exists[$key_to_check,$things] ? $things[$key_to_check] : NULL];
3Một mảng có các phím để kiểm tra.
Trả về giá trị
Trả về
function get_from_array[$key_to_check, $things]
return [array_key_exists[$key_to_check,$things] ? $things[$key_to_check] : NULL];
4 khi thành công hoặc function get_from_array[$key_to_check, $things]
return [array_key_exists[$key_to_check,$things] ? $things[$key_to_check] : NULL];
5 khi thất bại.function get_from_array[$key_to_check, $things]
return [array_key_exists[$key_to_check,$things] ? $things[$key_to_check] : NULL];
4 on success or function get_from_array[$key_to_check, $things]
return [array_key_exists[$key_to_check,$things] ? $things[$key_to_check] : NULL];
5 on failure. Ghi chú::
Array_Key_Exists [] sẽ chỉ tìm kiếm các khóa trong chiều đầu tiên. Các phím lồng nhau trong các mảng đa chiều sẽ không được tìm thấy. will search for the keys in the first dimension only. Nested keys in multidimensional arrays will not be found.
Ví dụ
Ví dụ #1 Array_Key_Exists [] Ví dụarray_key_exists[] example
function get_from_array[$key_to_check, $things]
return [array_key_exists[$key_to_check,$things] ? $things[$key_to_check] : NULL];
6Ví dụ #2 mảng_key_exists [] vs isset []array_key_exists[] vs isset[]
ISSET [] không trả về
4 cho các khóa mảng tương ứng với giá trị function get_from_array[$key_to_check, $things]
return [array_key_exists[$key_to_check,$things] ? $things[$key_to_check] : NULL];
8, trong khi Array_Key_Exists [] không. does not return function get_from_array[$key_to_check, $things]
return [array_key_exists[$key_to_check,$things] ? $things[$key_to_check] : NULL];
4 for array keys that correspond to a function get_from_array[$key_to_check, $things]
return [array_key_exists[$key_to_check,$things] ? $things[$key_to_check] : NULL];
8 value, while array_key_exists[] does. function get_from_array[$key_to_check, $things]
return [array_key_exists[$key_to_check,$things] ? $things[$key_to_check] : NULL];
function get_from_array[$key_to_check, $things]
return [array_key_exists[$key_to_check,$things] ? $things[$key_to_check] : NULL];
9Ghi chú
Ghi chú::
Array_Key_Exists [] sẽ chỉ tìm kiếm các khóa trong chiều đầu tiên. Các phím lồng nhau trong các mảng đa chiều sẽ không được tìm thấy.array_key_exists[] will also return
4 iffunction get_from_array[$key_to_check, $things] return [array_key_exists[$key_to_check,$things] ? $things[$key_to_check] : NULL];
2 is a property defined within an object given asfunction get_from_array[$key_to_check, $things] return [array_key_exists[$key_to_check,$things] ? $things[$key_to_check] : NULL];
3. This behaviour is deprecated as of PHP 7.4.0, and removed as of PHP 8.0.0.function get_from_array[$key_to_check, $things] return [array_key_exists[$key_to_check,$things] ? $things[$key_to_check] : NULL];
Để kiểm tra xem một thuộc tính có tồn tại trong một đối tượng hay không, nên sử dụng property_exists [].property_exists[] should be used.
Xem thêm
- ISSET [] - Xác định xem một biến được khai báo và khác với NULL
- Array_Keys [] - Trả về tất cả các phím hoặc một tập hợp con của các phím của một mảng
- in_array [] - kiểm tra xem giá trị có tồn tại trong một mảng không
- Property_exists [] - Kiểm tra xem đối tượng hoặc lớp có thuộc tính không
Manhon824 tại Gmail Dot Com ¶ ¶
11 năm trước
true if the key exists and the value is != NULL
3
true if the key exists and the value is != NULL
4
true if the key exists and the value is != NULL
5
true if the key exists and the value is != NULL
6