Home  >  Article  >  Backend Development  >  How to check whether a specified element in an associative array exists in php

How to check whether a specified element in an associative array exists in php

青灯夜游
青灯夜游Original
2023-01-09 14:09:471622browse

Checking method: 1. Use the in_array() function, the syntax is "in_array (specified element, array)", if it is included, it returns TRUE; 2. Use the array_search() function, the syntax is "array_search(value, array) ", if included, return the corresponding key name; 3. Use the foreach statement to loop through the array elements, and use the "$value===specified value" statement to determine whether the current array element is equal to the specified value. If equal, the array contains the value.

How to check whether a specified element in an associative array exists in php

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

php queries whether the array exists A certain value

Method 1: Use the in_array() function

In php, if you want to query whether a certain value exists in the array, you can use it directly Built-in function in_array()

in_array() function searches the array for the presence of a specified value. Syntax format:

in_array ( $search , $array ,$strict)
##ParameterDescriptionRequired. Specifies the value to search for in the array. Required. Specifies the array to search. Optional. If this parameter is set to TRUE, the in_array() function checks whether the data being searched is of the same type as the value of the array.
search
array
strict
Return value: TRUE if the value is found in the array, FALSE otherwise.


Example:


<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$Array = array(
    &#39;Chandler&#39; => 50, 
    &#39;Monica&#39; => 80, 
    &#39;Ross&#39; => 95
);
if (in_array("80", $Array)){
    echo "存在指定值";
}
else{
    echo "不存在指定值";
}
?>

How to check whether a specified element in an associative array exists in php

Method 2: Use array_search() function

The array_search() function searches for a key value in the array and returns the corresponding key name.

You can also use this function to query whether a certain value exists in the array. If it exists, the corresponding key name will be returned. If it does not exist, it will return false.

Syntax:


array_search(value,array,strict)

ParametersDescriptionRequired. Specifies the key value to search for in the array. Required. Specifies the array to be searched. Optional. If this parameter is set to TRUE, the function searches the array for elements of the same data type and value. Possible values:
value
array
strict
    true
  • false - default
  • If set to true, the type of the given value in the array is checked, the number 5 and the string 5 are different.
Return value:

  • If the specified key value is found in the array, return the corresponding key name, otherwise return FALSE . If a key value is found more than once in the array, the key name matching the first found key value is returned.

Example:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$Array = array(
    &#39;Chandler&#39; => 50, 
    &#39;Monica&#39; => 80, 
    &#39;Ross&#39; => 95
);
if (array_search("red", $Array)){
    echo "存在指定值";
}
else{
    echo "不存在指定值";
}
?>

How to check whether a specified element in an associative array exists in php

Method 3: Use foreach loop statement

  • Use the foreach statement to loop through the array

  • In the loop body, use the === operator to detect whether the specified value exists. That is, in each loop, determine whether the current array value $value is equal to the specified value

    • If it is equal, the array contains the value

    • If If not equal, the array does not contain the value

    ##
    <?php
    header(&#39;content-type:text/html;charset=utf-8&#39;);   
    $arr = array(
        &#39;Chandler&#39; => 50, 
        &#39;Monica&#39; => 80, 
        &#39;Ross&#39; => 95
    );
    var_dump($arr);
    foreach($arr as $value){
    	if($value===50){
    		echo "包含指定值";
    		break;
    	}
    }
    ?>

How to check whether a specified element in an associative array exists in php Recommended learning: "

PHP Video Tutorial

The above is the detailed content of How to check whether a specified element in an associative array exists 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