Home >Backend Development >PHP Problem >How to detect whether a specified subscript exists in an array in php

How to detect whether a specified subscript exists in an array in php

青灯夜游
青灯夜游Original
2022-07-25 18:53:053094browse

Two detection methods: 1. Use array_key_exists() to detect, the syntax is "array_key_exists (subscript value, array)". 2. Use array_keys() to obtain all subscripts (key names) of the original array and return an array of key names. Use array_search() to search for the specified value in the key name array. The syntax is "array_search("a", array_keys(original Array))", returns the corresponding key name if it exists, returns FALSE if it does not exist.

How to detect whether a specified subscript exists in an array in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer

Array array is a set of ordered variables. Each value is called an element. Each element is distinguished by a special identifier called a key (also called a subscript).

Each entity in the array contains two items, namely key and value. The corresponding array elements can be obtained by key value. These keys can be numeric keys or association keys. If a variable is a container that stores a single value, then an array is a container that stores multiple values.

Two methods for php to detect whether the specified subscript exists in an array

##Method 1: Use the array_key_exists() function to detect

array_key_exists() function checks whether the specified key name exists in an array. If the key name exists, it returns true. If the key name does not exist, it returns false.

array_key_exists(key,array)

ParametersDescriptionRequired . Specifies the key name. Required. Specifies an array.
key
array
Example: Specify whether the subscript 'a' exists


<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr=array("a"=>"Dog","b"=>"Cat");
var_dump($arr);
if (array_key_exists("a",$arr)){
	echo "指定下标&#39;a&#39;存在!";
}else
{
	echo "指定下标&#39;a&#39;不存在";
}
?>

How to detect whether a specified subscript exists in an array in php

Method 2: Use array_keys() and array_search() to detect

  • First use the array_keys() function to obtain all subscripts (key names) of the original array and return an array of key names

  • Use the array_search() function to search for the specified value (the subscript of the original array) in the key array.

    • If the specified value exists, return the corresponding key name

    • If the specified value does not exist, return FALSE

  • <?php
    header(&#39;content-type:text/html;charset=utf-8&#39;);   
    $arr=array("a"=>"Dog","b"=>"Cat");
    echo "原数组:";
    var_dump($arr);
    
    $keys=array_keys($arr);
    echo "键名数组:";
    var_dump($keys);
    var_dump(array_search("a",$keys));
    var_dump(array_search("b",$keys));
    var_dump(array_search("c",$keys));
    ?>

How to detect whether a specified subscript exists in an array in php

Recommended learning: "

PHP Video Tutorial"

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

Statement:
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