Home  >  Article  >  Daily Programming  >  PHP checks if all values ​​in array are strings

PHP checks if all values ​​in array are strings

藏色散人
藏色散人Original
2019-01-15 15:35:115899browse

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.

PHP checks if all values ​​in array are strings

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(&#39;is_string&#39;,$arr)) == count($arr);
}
$arr1 = array(&#39;PHP&#39;,&#39;Js&#39;,&#39;Python&#39;);
$arr2 = array(&#39;SQL&#39;,200,&#39;MySQL&#39;);

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:

PHP checks if all values ​​in array are strings

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!

Statement:
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