Home >Backend Development >PHP Problem >How to determine the function of an array in php
PHP is a server-side scripting language for Web application development. It is easy to learn, open source, free and highly portable. It is widely used in the field of Web development. In the process of PHP development, it is often necessary to operate and judge arrays, so it is very important to master the function of judging arrays in PHP.
1. is_array function
The is_array function is the most basic function for judging arrays in PHP. It can judge whether the incoming variable is an array type. If so, it returns true, otherwise it returns false. The format of this function is as follows:
bool is_array (mixed $var)
Among them, $var represents the variable to be judged, which can be of any type. The following is a sample code for the is_array function:
<?php $arr = array(1, 2, 3); if (is_array($arr)) { echo '$arr is an array'; } else{ echo '$arr is not an array'; } ?>
This code will output: $arr is an array. This is because $arr is an array, so the is_array function returns true.
2. array_key_exists function
The array_key_exists function is used to determine whether the specified key value exists in the array. It can determine whether the specified key value exists in the array and returns true if it exists. Otherwise return false. The format of this function is as follows:
bool array_key_exists (mixed $key, array $array)
Among them, $key represents the key value to be judged, and $array represents the array with judgment. The following is a sample code for the array_key_exists function:
<?php $arr = array('name' => 'Tom', 'age' => 20); if (array_key_exists('name', $arr)) { echo '$arr has key name'; } else{ echo '$arr does not have key name'; } ?>
This code will output: $arr has key name. This is because there is an element with the key value name in $arr, so the array_key_exists function returns true.
3. in_array function
The in_array function is used to determine whether the specified value exists in the array. It can determine whether the specified value exists in the array. If it exists, it returns true, otherwise it returns false. The format of this function is as follows:
bool in_array (mixed $value, array $array [, bool $strict])
Among them, $value represents the value to be judged, and $array represents the value to be judged. Array, $strict indicates whether to perform strict comparison (that is, whether the comparison type and value are equal), and the default is false. The following is a sample code for the in_array function:
<?php $arr = array('apple', 'orange', 'banana'); if (in_array('apple', $arr)) { echo '$arr contains apple'; } else{ echo '$arr does not contain apple'; } ?>
This code will output: $arr contains apple. This is because $arr contains an element with the value apple, so the in_array function returns true.
4. array_search function
The array_search function is similar to the in_array function. It is also used to find the specified value in the array. The difference is that it returns the first occurrence of the specified value in the array. Index position (subscript), if not found, returns false. The format of this function is as follows:
mixed array_search (mixed $needle, array $haystack [, bool $strict])
Among them, $needle represents the value to be found, and $haystack represents the value to be found. array, $strict indicates whether to perform strict comparison, and the default is false. The following is a sample code for the array_search function:
<?php $arr = array('apple', 'orange', 'banana'); $index = array_search('orange', $arr); if ($index !== false) { echo "The index of orange is $index"; } else{ echo "orange is not in the array"; } ?>
This code will output: The index of orange is 1. This is because the index position of the first element with value orange in $arr is 1, so the array_search function returns 1.
5. Count function
The count function is used to count the number of elements in the array. It can be used to determine whether the array is empty. The format of this function is as follows:
int count (mixed $var [, int $mode = COUNT_NORMAL])
Among them, $var represents the variable to be calculated, which can be an array, object or other Type variable, $mode represents the calculation method, optional parameter, the default is COUNT_NORMAL, which means not to recursively calculate multi-dimensional arrays. The following is a sample code for the count function:
<?php $arr1 = array(); $arr2 = array(1, 2, 3); if (count($arr1) == 0) { echo "arr1 is empty"; } else{ echo "arr1 has " . count($arr1) . " elements"; } if (count($arr2) == 0) { echo "arr2 is empty"; } else{ echo "arr2 has " . count($arr2) . " elements"; } ?>
This code will output: arr1 is empty arr2 has 3 elements. This is because $arr1 is an empty array, and the count function returns 0, so the output arr1 is empty; there are 3 elements in $arr2, and the count function returns 3, so the output arr2 has 3 elements.
6. isset function
isset function is used to detect whether the variable has been set and is not NULL. It can determine whether the specified subscript in the array exists. The format of this function is as follows:
bool isset (mixed $var [, mixed $...])
Among them, $var represents the variable to be detected, which can be of any type, $. .. means that multiple variables to be detected can be passed in. The following is a sample code for the isset function:
<?php $arr = array('name' => 'Tom', 'age' => 20); if (isset($arr['name'])) { echo '$arr has key name'; } else{ echo '$arr does not have key name'; } ?>
This code will output: $arr has key name. This is because there is an element with the key name name in $arr, and the isset function returns true.
Summary
There are many functions for judging arrays in PHP, including is_array, array_key_exists, in_array, array_search, count, isset, etc. Using these functions can easily operate and judge arrays, improving development efficiency and code readability. It should be noted that the parameters and return value types of these functions are different. Please check the official PHP documentation carefully when using them.
The above is the detailed content of How to determine the function of an array in php. For more information, please follow other related articles on the PHP Chinese website!