preg_grep()
will not work with multi-dimensional arrays. You'll have to fetch the keys separately using array_keys()
and apply preg_grep()
on that. Now use array_intersect_key()
in conjunction with array_flip()
to create the result array:
$keys = preg_grep('/[XLS]/', array_keys($testArray));
$result = array_intersect_key($testArray, array_flip($keys));
Output:
Array
(
[XXL] => Array
(
)
[XL] => Array
(
)
[L] => Array
(
)
[S] => Array
(
)
[XS] => Array
(
)
)