Làm cách nào để kiểm tra kiểu mảng trong PHP?
Hành vi mặc định của $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }9 là so sánh các mục bằng cách sử dụng toán tử $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }0. Để sử dụng kiểm tra đẳng thức nghiêm ngặt, $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }1, hãy chuyển $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }2 làm tham số thứ ba cho $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }9 Show
$array = array(1, '2', 'three'); in_array(0, $array); // true! in_array(0, $array, true); // false in_array(1, $array); // true in_array(1, $array, true); // true in_array(2, $array); // true in_array(2, $array, true); // false Lần kiểm tra đầu tiên, $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }1, đánh giá là $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }2 vì để so sánh số $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }3 với chuỗi $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }4, PHP chuyển đổi $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }4 thành một số nguyên. Vì $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }4 không phải là một chuỗi số, cũng như $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }7, nó trở thành $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }3. Do đó, $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }9 nghĩ rằng có sự trùng khớp Do đó, khi so sánh các số với dữ liệu có thể chứa chuỗi, cách an toàn nhất là sử dụng phép so sánh nghiêm ngặt Nếu bạn thấy mình gọi $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }9 nhiều lần trên cùng một mảng, có thể tốt hơn là sử dụng mảng kết hợp, với các phần tử mảng ban đầu làm khóa trong mảng kết hợp mới. Tra cứu các mục sử dụng $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }9 mất thời gian tuyến tính; Nếu bạn không thể trực tiếp tạo mảng kết hợp mà cần chuyển đổi từ mảng truyền thống bằng các khóa số nguyên, hãy sử dụng $array = array(1, '2', 'three'); in_array(0, $array); // true! in_array(0, $array, true); // false in_array(1, $array); // true in_array(1, $array, true); // true in_array(2, $array); // true in_array(2, $array, true); // false2 để hoán đổi khóa và giá trị của một mảng $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); // convert from numeric array to associative array $book_collection = array_flip($book_collection); $book = 'Sense and Sensibility'; if (isset($book_collection[$book])) { echo 'Own it.'; } else { echo 'Need it.'; } Lưu ý rằng việc làm này sẽ cô đọng nhiều khóa có cùng giá trị thành một phần tử trong mảng đã lật Đây là Công thức mã để kiểm tra xem mảng #JavaScript có chứa giá trị không. Bạn có thể sử dụng phương thức mảng mới $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }03 😋 Đối với các trình duyệt cũ hơn và IE, bạn có thể sử dụng $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }04 👍 $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }2 # $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }03 với các kiểu nguyên thủy khácBên cạnh các chuỗi, $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }03 cũng hoạt động tốt với các kiểu nguyên thủy khác $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }0 Sử dụng $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }03 $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }7 Sử dụng $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }04 $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }9 # Lưu ý của $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }04Cho đến giờ, tôi đã cho bạn thấy các giá trị mà cả $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }03 và $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }04 đều có thể thay thế cho nhau. Tuy nhiên, có một giá trị, nơi chúng khác nhau 🤭 $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }3 # Kiểm tra Mảng đối tượng bằng cách sử dụng $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }72Để có giải pháp linh hoạt hơn hoạt động trên các loại dữ liệu khác, bạn có thể muốn sử dụng $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }73 thay thế
$book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }6 Phương pháp này là lý tưởng cho một mảng các đối tượng $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }7 Trong một ghi chú mã trước, tôi đã nói về một cách nhanh chóng và bẩn để kiểm tra $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }74 bằng cách sử dụng $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }75
Lấy khái niệm đó, chúng ta cũng có thể sử dụng nó để so sánh phần tử đối tượng trong một mảng như thế này $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }0 # Trường hợp nhạy cảmCả $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }03 và $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }04 đều phân biệt chữ hoa chữ thường $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }3 Để làm cho nó không phân biệt chữ hoa chữ thường, bạn có thể cân nhắc thay đổi chữ hoa chữ thường của mảng như vậy $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }4 Nhưng nếu bạn đang sử dụng $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }73, bạn có thể làm điều đó trong một dòng $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }00 # Hỗ trợ trình duyệtHỗ trợ cho $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }03 thực sự tốt cho tất cả các trình duyệt hiện đại. Tuy nhiên, nếu bạn cần IE hoặc trình duyệt cũ hơn, bạn sẽ cần sử dụng $book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }04 Tôi có thể sử dụng không? . nguyên mẫu. bao gồm # Đầu vào của cộng đồng
$book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }01
$book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey'); $book = 'Sense and Sensibility'; if (in_array($book, $book_collection) { echo 'Own it.'; } else { echo 'Need it.'; }02
|