I have an array of objects, and I want to check if a certain classname is in it. So I tried:
$all_classnames = array_filter($obj_array, 'get_class');
$found = in_array("classname_to_test", $all_classnames);
Only, $all_classnames still holds the original object array instead of an array of classnames (through get_class). Am I missing something here?
You want to use array_map
(which transforms the input array based on the callback function) instead of array_filter
:
$all_classnames = array_map('get_class', $obj_array);
Note that array_map
takes its arguments in the reverse order than the other array functions that use a callback because PHP.
This is not how the array_filter function works. It just filters your array if the callback returns false
it will remove the element from the array.
What you need is array_map
$all_classnames = array_map('get_class', $obj_array);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.