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

$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);  // false
2 để 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ác

Bê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.'; }04

Cho đế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ế

". một số()". kiểm tra xem ít nhất một phần tử trong mảng có vượt qua bài kiểm tra được thực hiện bởi hàm được cung cấp hay không. Nó trả về một giá trị Boolean

$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

Cách so sánh 2 đối tượng trong JavaScript

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ả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 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ệt

Hỗ 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

  • @lolinoid.

    $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.';
    }
    91 > @prvnbist Đó là một phương thức DOM Nodes, ví dụ được biết đến nhiều nhất về nó sẽ nhận được một danh sách các tên lớp sẽ là một danh sách nút, sau đó bạn có thể sử dụng phương thức chứa để xem nó có tên lớp hay không. Hoặc bạn có thể chuyển đổi nó thành một mảng và sau đó sử dụng phương thức bao gồm

  • Bạn có thể 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.';
    }
    92 để lặp qua một đối tượng để kiểm tra xem có tồn tại khóa thuộc tính cụ thể hay không. (Kiến thức được chia sẻ bởi @fvbix)

$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

  • @pixelfixer. Và nếu bạn muốn giá trị được trả về, 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.';
    }
    93

  • Kiểm tra hiệu suất> mảng indexOf so với bao gồm. Cảm ơn @equiman

  • @smokku. Bạn có thể tránh tất cả các so sá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.';
    }
    94 này 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.';
    }
    95

$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

  • @danvc.

    $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.';
    }
    96. 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.';
    }
    95 theo bit sẽ trả về giá trị trung thực cho bất kỳ thứ gì ngoại trừ
    $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.';
    }
    98. Phủ định nó cũng đơn giản như làm
    $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.';
    }
    99

  • @smokku. Bitwise

    $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.';
    }
    30 cung cấp cho bạn số ngược lại, nhưng chúng tôi sử dụng hệ thống bổ sung của hai để tránh 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.';
    }
    31 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.';
    }
    32. Vì vậy, các số âm được dịch chuyển một - trong đó
    $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.';
    }
    98 thay thế 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.';
    }
    32. Khi chúng tôi phủ đị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.';
    }
    98, chúng tôi nhận đượ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.';
    }
    31, đây là giá trị falsey duy nhất. Tất cả các chỉ số khác mà chúng tôi có thể nhận được 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.';
    }
    37 sẽ cung cấp cho bạn giá trị trung thực (khác không) khi được chuyển qua bitwise chứ không phả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.';
    }
    95

  • @smokku. Nếu bạn muốn tìm hiểu sâu về vấn đề này, tôi thực sự khuyên bạn nên đọc một cuốn sách của Charles Petzold có tựa đề "Mật mã. Ngôn ngữ ẩn của phần cứng và phần mềm máy tính". Đây là một cuốn sách thú vị, nó sẽ hướng dẫn bạn từ những kiến ​​thức cơ bản về thiết kế mã (như Morse, Braile, v.v. ) thông qua các công tắc điện và bóng đèn nhấp nháy, điện báo, cổng điện tử và dép xỏ ngón, cho đến cấp độ CPU đơn giản

    Làm cách nào để kiểm tra kiểu dữ liệu trong PHP?

    Hàm gettype() là một hàm có sẵn trong PHP dùng để lấy kiểu của một biến. Nó được sử dụng để kiểm tra loại biến hiện có. Tham số. Hàm này chấp nhận một tham số duy nhất $var. Là tên biến cần kiểm tra kiểu của biến.

    Mảng có phải là kiểu dữ liệu trong PHP không?

    Mảng PHP. Mảng là kiểu dữ liệu phức hợp . Nó có thể lưu trữ nhiều giá trị của cùng một kiểu dữ liệu trong một biến duy nhất.

    Làm cách nào để kiểm tra kích thước mảng trong PHP?

    Redis và PHP . Phải mất một tham số, tôi. e mảng cần kiểm tra và trả về có hoặc không tùy thuộc vào bản chất của mảng. The 'rsort' function can be used to check if an array is multidimensional or not. It takes one parameter, i.e the array that needs to be checked and returns yes or no depending on the nature of the array.

    Array_keys() dùng để làm gì trong PHP?

    Mảng_keys() là một hàm tích hợp sẵn trong PHP và được sử dụng để trả về tất cả các khóa của và mảng hoặc tập hợp con của các khóa.