PHP checks whether all the values in the array are strings. We can use the built-in functions array_sum, array_map, is_string and other related functions in PHP to achieve this.
Below we will combine specific code examples to introduce to you how PHP determines whether all the values in the array are strings.
The code example is as follows:
<?php function check_strings_in_array($arr) { return array_sum(array_map('is_string',$arr)) == count($arr); } $arr1 = array('PHP','Js','Python'); $arr2 = array('SQL',200,'MySQL'); var_dump(check_strings_in_array($arr1)); var_dump(check_strings_in_array($arr2));
Here we determine whether there are strings in the $arr1 and $arr2 arrays.
The judgment results are as follows:
Related function introduction:
array_sum —Yes Sum all the values in the array
array_sum ( array $array ) : number
array_sum() Adds all the values in the array and returns the result.
Parameter array, input array.
Return value, the sum of all values is returned as an integer or floating point number, and 0 is returned when the array is empty.
array_map — Apply the callback function to each element of the array
array_map ( callable $callback , array $array1 [, array $... ] ) : array
array_map(): Returns the array, which is the array after applying the callback function to each element of array1. The number of callback function parameters and the number of arrays passed to array_map() must be the same.
Parameters, callback function, apply to each element in each array.
array1 array, traverse and run the callback function.
...
Array list, each traversing and running callback function.
Return value, return array, containing all elements of array1 after callback function processing.
is_string —Detect whether the variable is a string
count —Count the number of cells in the array, or the number of attributes in the object
This article is about PHP's method of checking whether all the values of an array are strings. I hope it will be helpful to friends in need!
The above is the detailed content of PHP checks if all values in array are strings. For more information, please follow other related articles on the PHP Chinese website!