note 115774 deleted from function.array-sum by salathe
| From: | salathe@php.net | Date: | Mon, 21 Sep 2020 14:51:32 +0000 |
| Subject: | note 115774 deleted from function.array-sum by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215357@lists.php.net to get a copy of this message | ||
Note Submitter: didatus at dynarize dot de
----
If you want to check if there are for example only strings in an array, you can use a combination of
array_sum and array_map like this:
<?php
function only_strings_in_array($arr) {
return array_sum(array_map('is_string', $arr)) == count($arr);
}
$arr1 = array('one', 'two', 'three');
$arr2 = array('foo', 'bar', array());
$arr3 = array('foo', array(), 'bar');
$arr4 = array(array(), 'foo', 'bar');
var_dump(
only_strings_in_array($arr1),
only_strings_in_array($arr2),
only_strings_in_array($arr3),
only_strings_in_array($arr4)
);
?>
This will give you the following result:
bool(true)
bool(false)
bool(false)
bool(false)