Home > Backend Development > PHP Problem > PHP array learning how to check whether there is a specified key name/value

PHP array learning how to check whether there is a specified key name/value

青灯夜游
Release: 2023-03-11 21:16:02
Original
2500 people have browsed it

In the previous article "PHP array learning: How to traverse array elements? In "A Brief Analysis of 4 Methods", we introduced 4 methods of traversing arrays. Today we will take a look at the key names and key values ​​of array elements, and introduce the method of checking whether the specified key name/value exists in a PHP array.

First let’s take a look at How to detect whether the key name is located in the array? In fact, it is very simple. We can use the built-in function array_key_exists() to detect it.

The array_key_exists() function can check whether the specified key name (or index) exists in an array; this function accepts two parameters $key and $array, which are used to specify the key name and array respectively. Returns true if the key name $key exists in the array $array, and returns false if it does not exist.

Let’s take a closer look through code examples.

<?php
header("Content-type:text/html;charset=utf-8");
$array= array("姓名"=>"张三","年龄"=>25,"性别"=>"男");
var_dump($array);
$key = &#39;年龄&#39;;
if( array_key_exists($key, $array) ){  //检测数组中是否存在该键
    echo "键名 &#39;$key&#39; 存在于数组中!";
}
?>
Copy after login

Output result:

PHP array learning how to check whether there is a specified key name/value

Main difference between isset() and array_key_exists() functions:

## The #array_key_exists() function will explicitly tell whether the key exists in the array, while isset() will only return true if the key/variable exists and is not null. Also, isset() does not render an error when the array/variable does not exist, while array_key_exists() does.

Next let’s take a look

How to detect whether the key value is located in the array? This is also simple. We can use the built-in function in_array() to detect. If a given value is found in a given array, it will return TRUE, otherwise it will return FALSE.

in_array($value,$array,$type)You can check whether the specified value exists in the specified array $array$value; The $type parameter can be omitted.

Let’s take a closer look through code examples.

<?php
header("Content-type:text/html;charset=utf-8");
$array= array("姓名"=>"张三","年龄"=>25,"性别"=>"男");
var_dump($array);
$value = &#39;张三&#39;;
if( in_array($value, $array) ){  //检测数组中是否存在该键名
    echo "键名 &#39;$value&#39; 存在于数组中!";
}
?>
Copy after login

Output result:

PHP array learning how to check whether there is a specified key name/value

The third parameter of the in_array() function

$type is omitted, but if set If the value is true, it will be checked whether the type of the searched data and the value of the array are the same. At this point, the function returns true only if the element exists in the array and has the same data type as the given value.

<?php
header("Content-type:text/html;charset=utf-8");
$array= array("姓名"=>"张三","年龄"=>25,"性别"=>"男");
var_dump($array);
$value = "25";
if( in_array($value, $array,true) ){  //检测数组中是否存在该键名
    echo "键名 &#39;$value&#39; 存在于数组中!";
}else{
	echo "键名 &#39;$value&#39; 不存在于数组中!";
}
echo "<br>";

$value = 25;
if( in_array($value, $array,true) ){  //检测数组中是否存在该键名
    echo "键名 $value 存在于数组中!";
}else{
	echo "键名 &#39;$value&#39; 不存在于数组中!";
}
?>
Copy after login
Output result:

PHP array learning how to check whether there is a specified key name/value

And if the

$value parameter is a string, and the $type parameter Set to true to make the search case-sensitive.

Okay, that’s all. If you want to know anything else, you can click this. → →

php video tutorial

Recommended: PHP interview questions summary (collection)

The above is the detailed content of PHP array learning how to check whether there is a specified key name/value. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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