Home > Backend Development > PHP Problem > How to verify if array subscript exists in php

How to verify if array subscript exists in php

PHPz
Release: 2023-04-20 14:57:42
Original
715 people have browsed it

In PHP, array is a commonly used data structure. When we need to use an array, we often need to determine whether a certain subscript already exists in the array in order to perform related operations. This article will introduce several methods to verify whether an array subscript exists.

Use isset() function

isset() function is a function used to detect whether a variable has been declared and assigned a value. It can be used to detect whether the array subscript already exists.

The following is a sample code that uses the isset() function to verify whether the array subscript exists:

<?php
$array = array(&#39;a&#39; => 1, 'b' => 2, 'c' => 3);

if(isset($array['a'])){
    echo "数组下标a存在";
} else {
    echo "数组下标a不存在";
}

if(isset($array['d'])){
    echo "数组下标d存在";
} else {
    echo "数组下标d不存在";
}
?>
Copy after login

The output result is:

数组下标a存在
数组下标d不存在
Copy after login
Copy after login
Copy after login

Use the array_key_exists() function

Similar to the isset() function, the array_key_exists() function can also be used to verify whether the array subscript exists. Compared with the isset() function, the advantage of the array_key_exists() function is that it can detect when the value of a certain subscript in the array is NULL.

The following is a sample code that uses the array_key_exists() function to verify whether the array subscript exists:

<?php
$array = array(&#39;a&#39; => 1, 'b' => 2, 'c' => null);

if(array_key_exists('a', $array)){
    echo "数组下标a存在";
} else {
    echo "数组下标a不存在";
}

if(array_key_exists('c', $array)){
    echo "数组下标c存在";
} else {
    echo "数组下标c不存在";
}
?>
Copy after login

The output result is:

数组下标a存在
数组下标c存在
Copy after login

Use the in_array() function

The in_array() function is a function used to determine whether a value exists in an array. When you need to determine whether a subscript exists, you can compare the subscript with all the key names in the array. If it exists, it indicates that the Subscript exists.

The following is a sample code that uses the in_array() function to verify whether the array subscript exists:

<?php
$array = array(&#39;a&#39; => 1, 'b' => 2, 'c' => 3);

if(in_array('a', array_keys($array))){
    echo "数组下标a存在";
} else {
    echo "数组下标a不存在";
}

if(in_array('d', array_keys($array))){
    echo "数组下标d存在";
} else {
    echo "数组下标d不存在";
}
?>
Copy after login

The output result is:

数组下标a存在
数组下标d不存在
Copy after login
Copy after login
Copy after login

Use the array_search() function

Similar to the in_array() function, the array_search() function is also a function used to find whether a certain value exists in an array. Its return value is the subscript corresponding to the value, or false if it does not exist.

The following is a sample code that uses the array_search() function to verify whether the array subscript exists:

<?php
$array = array(&#39;a&#39; => 1, 'b' => 2, 'c' => 3);

if(array_search('a', $array)){
    echo "数组下标a存在";
} else {
    echo "数组下标a不存在";
}

if(array_search('d', $array)){
    echo "数组下标d存在";
} else {
    echo "数组下标d不存在";
}
?>
Copy after login

The output result is:

数组下标a存在
数组下标d不存在
Copy after login
Copy after login
Copy after login

Summary

This article This article introduces four methods for verifying the existence of array subscripts in PHP, including using the isset() function, array_key_exists() function, in_array() function and array_search() function. The application of these methods can help us better operate arrays in actual development and improve the readability and safety of the code.

The above is the detailed content of How to verify if array subscript exists in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template