note 115774 added to function.array-sum
| From: | didatus at dynarize dot de | Date: | Sat, 20 Sep 2014 21:21:19 +0000 |
| Subject: | note 115774 added to function.array-sum | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-200493@lists.php.net to get a copy of this message | ||
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)
----
Server IP: 212.124.37.9
Probable Submitter: 5.146.27.132
----
Manual Page -- http://php.net/manual/en/function.array-sum.php
Edit -- https://master.php.net/note/edit/115774
Del: integrated -- https://master.php.net/note/delete/115774/integrated
Del: useless -- https://master.php.net/note/delete/115774/useless
Del: bad code -- https://master.php.net/note/delete/115774/bad+code
Del: spam -- https://master.php.net/note/delete/115774/spam
Del: non-english -- https://master.php.net/note/delete/115774/non-english
Del: in docs -- https://master.php.net/note/delete/115774/in+docs
Del: other reasons-- https://master.php.net/note/delete/115774
Reject -- https://master.php.net/note/reject/115774
Search -- https://master.php.net/manage/user-notes.php