Home > Backend Development > PHP Problem > How to determine if a variable is in an array in php

How to determine if a variable is in an array in php

PHPz
Release: 2023-04-19 10:48:29
Original
681 people have browsed it

PHP is a powerful programming language that is widely used in web development. Among them, array is one of the most common data types in PHP, which allows developers to store multiple values ​​in one variable.

In PHP, sometimes it is necessary to determine whether a variable is an array so that different operations can be taken in different situations. This article will introduce several methods to determine whether a variable is an array.

1. Use the is_array() function

The is_array() function is a function in PHP used to determine whether a variable is an array. Its syntax is as follows:

bool is_array (mixed $var)

Among them, $var is the variable to be judged. Returns true if $var is an array; otherwise returns false.

Sample code:

$arr ​​= array(1, 2, 3);
if (is_array($arr)) {

echo '$arr is an array.';
Copy after login
Copy after login

} else {

echo '$arr is not an array.';
Copy after login
Copy after login

}
?>

Output result:

$arr ​​is an array.

2.Use gettype() function

gettype() function is a function used in PHP to obtain the variable type. Its syntax is as follows:

string gettype (mixed $var)

Among them, $var is the variable to get the type. If $var is an array, returns "array"; otherwise, returns the variable's type.

Sample code:

$arr ​​= array(1, 2, 3);
if (gettype($arr) === 'array' ) {

echo '$arr is an array.';
Copy after login
Copy after login

} else {

echo '$arr is not an array.';
Copy after login
Copy after login

}
?>

Output result:

$arr ​​is an array.

3. Use the array_key_exists() function

The array_key_exists() function is a function in PHP used to determine whether a specified key name exists in an array. Returns true if the specified key exists; otherwise returns false.

Sample code:

$arr ​​= array('name' => 'Tom', 'age' => 20);
if (array_key_exists('name', $arr)) {

echo '$arr has "name" key.';
Copy after login

} else {

echo '$arr does not have "name" key.';
Copy after login

}
?>

Output result:

$arr ​​has "name" key.

If you want to determine whether a variable is an array, you can use the array_key_exists() function to determine whether its key name exists. If present, the variable is an array.

Sample code:

$data = 'hello';
if (array_key_exists('0', $data)) {

echo '$data is an array.';
Copy after login

} else {

echo '$data is not an array.';
Copy after login

}
?>

Output result:

$data is not an array.

The above is a few A way to determine whether a PHP variable is an array. Developers can choose a method that suits them based on actual needs.

The above is the detailed content of How to determine if a variable is in an array 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