Home >Backend Development >PHP Problem >What are the common methods in php arrays
Commonly used methods in php arrays are: 1. is_array; 2. in_array; 3. array_key_exists; 4. array_search; 5. array_keys.
Commonly used array methods:
(Learning video recommendation: java video tutorial)
1 , is_array — Check whether a variable is an array
Syntax:
bool is_array ( mixed $var ) //如果 var 是 array,则返回 TRUE,否则返回 FALSE。
Usage:
$arr = []; $arr1 = 99; var_dump(is_array($arr)); //输出 bool(true) var_dump(is_array($arr1)); //输出 bool(false) 类似的方法: 1)、is_int — 检测变量是否是整数 bool is_int ( mixed $var ) //如果 var 是 integer 则返回 TRUE,否则返回 FALSE。 is_integer — is_int() 的别名 2)、is_numeric — 检测变量是否为数字或数字字符串 bool is_numeric ( mixed $var ) //如果 var 是数字和数字字符串则返回 TRUE,否则返回 FALSE。 Note:若想测试一个变量是否是数字或数字字符串(如表单输入,它们通常为字符串),必须使用 is_numeric()。 3)、is_bool — 检测变量是否是布尔型 bool is_bool ( mixed $var ) //如果 var 是 boolean 则返回 TRUE。 4)、is_float — 检测变量是否是浮点型 bool is_float ( mixed $var ) //如果 var 是 float 则返回 TRUE,否则返回 FALSE。 is_real — is_float() 的别名 5)、is_string — 检测变量是否是字符串 bool is_string ( mixed $var ) //如果 var 是 string 则返回 TRUE,否则返回 FALSE。 6)、is_object — 检测变量是否是一个对象 bool is_object ( mixed $var ) //如果 var 是一个 object 则返回 TRUE,否则返回 FALSE。
2, in_array — Check whether a value exists in the array
Syntax:
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) //大海捞针,在大海(haystack)中搜索针( needle),如果没有设置 strict 则使用宽松的比较。
Parameters:
needle The value to be searched. (If needle is a string, the comparison is case-sensitive.)
haystack The array to search.
strict If the value of the third parameter strict is TRUE, the in_array() function will also check whether the type of needle is the same as that in haystack.
Return value:
If needle is found, TRUE is returned, otherwise FALSE is returned.
Example:
//区分大小写 $fruits = [ "Apple", "Pear", "Bana", "Orange" ]; if (in_array("Apple", $fruits)) { echo "Apple "; } if (in_array("apple", $fruits)) { echo "apple "; } //开启严格检查 $number = [ 13, 14, 15, 16 ]; if (in_array("13", $number, true)) { echo "string 13"; } if (in_array(13, $number, true)) { echo "int 13"; } 返回:Apple int 13
3. array_key_exists - Check whether there is a specified key name or index in the array
Syntax:
bool array_key_exists ( mixed $key , array $array ) // 数组里有键 key 时,array_key_exists() 返回 TRUE。 key 可以是任何能作为数组索引的值。
Parameter description:
key The key to be checked
array An array containing the key to be checked
Return value: TRUE on success, or FALSE on failure.
Example:
$array = [ 1,2,3,4 ]; var_dump(array_key_exists(0, $array)); //输出 bool(true)
4, array_search - Search for a given value in the array, if successful, return the first corresponding key name,
Syntax:
mixed array_search ( mixed $needle , array $haystack [, bool $strict = false ] ) //大海捞针,在大海(haystack)中搜索针( needle 参数)。
Parameter description:
needle search value. (If needle is a string, the comparison is case-sensitive. )
haystack this array.
strict If the optional third argument strict is TRUE, array_search() will check for identical elements in the haystack.
This means that the type of needle in haystack is also strictly compared, and the objects must be the same instance.
Return value:
If needle is found, return its key, otherwise return FALSE.
If needle appears more than once in haystack, return the first matching key. To return all keys that match a value, array_keys() with the optional argument search_value should be used instead.
Example:
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red'); $key = array_search('green', $array); // $key = 2; $key = array_search('red', $array); // $key = 1;
5, array_keys - Return some or all key names in the array
Syntax:
array array_keys ( array $array [, mixed $search_value = null [, bool $strict = false ]] )
If optional parameters are specified search_value, only the key name of the value is returned. Otherwise all keys in the input array will be returned.
Parameter description:
input An array containing the keys to be returned.
search_value If this parameter is specified, only keys containing these values will be returned.
strict Determines whether strict comparison (===) should be used when searching.
Return value: Return all keys in input.
Example:
$array = array(0 => 100, "color" => "red"); print_r(array_keys($array)); $array = array("blue", "red", "green", "blue", "blue"); print_r(array_keys($array, "blue")); $array = array("color" => array("blue", "red", "green"), "size" => array("small", "medium", "large")); print_r(array_keys($array)); 返回: Array ( [0] => 0 [1] => color ) Array ( [0] => 0 [1] => 3 [2] => 4 ) Array ( [0] => color [1] => size )
Related recommendations: php training
The above is the detailed content of What are the common methods in php arrays. For more information, please follow other related articles on the PHP Chinese website!