简体   繁体   中英

PHP - How to check if array contains a value by index?

Example: $array = ['name', 'address'];

It contains $array[0] and $array[1] only.

If i try $array[2] i should receive an error.

So, i want to avoid these errors by checking it in a if statement. How can i check if an array contains a value in certain index?

您可以使用isset函数:

if(isset($array[2])) {

使它起作用的第二种方法是检查数组的大小:

if(count($array) >= 3) { ... } // three or more elements: 0, 1, 2...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM