php - Check if object contains an property matching a string -
stdclass object ( [id] => 11 [type] => 5 [color_a] => 57 [color_b] => 3 [date] => 2 )
how check if object has attributes contain string "color" ?
i tried array_diff_key , array_filter cannot use array_filter_use_key because runs on php 5.4.
no loops if possible :)
this should work you:
(here cast object array , search preg_grep()
in array_keys()
. flip array array_flip()
)
$result = array_flip(preg_grep("/\bcolor/", array_keys((array)$o))); print_r($result);
output:
array ( [color_a] => 2 [color_b] => 3 )
and if want use check true or false in_array() don't need flip , can use this:
if(preg_grep("/\bcolor/", array_keys((array)$o))) echo "match";
Comments
Post a Comment