One of several approaches that can be used for this is to use the preg_grep
to return the items that match a pattern, and to do so, enter the array keys with the array_keys
:
$numeros = array('one' => 1, 'two' => 2, 'three' => 3, 'my_one' => 55, 'my_two' => 33);
$myNumeros = preg_grep('/^my_.*/', array_keys($numeros));
print_r($myNumeros); // [3] => my_one [4] => my_two
View demonstração
Another way to do this is to use the array_filter
function to filter the keys in the array using the array_keys
function, and in the callback / a>, compare the value with strpos
(or stripos
for case-insensitive ):
$numeros = array('one' => 1, 'two' => 2, 'three' => 3, 'my_one' => 55, 'my_two' => 33);
$myNumeros = array_filter(array_keys($numeros), function ($chave){
return (strpos($chave, 'my_') !== false);
});
print_r($myNumeros); // [3] => my_one [4] => my_two
View demonstração