You can use a negative filter (with strpos
) instead:
foreach ($array as $key => $value) { if (strpos($key, 'array_') !== 0) { unset($array[$key]); }}
Note that it modifies the array in-place.
Update
Or, since 5.6 you can now use array_filter
:
$array = array_filter($array, function($value, $key) { return strpos($key, 'array_') === 0;}, ARRAY_FILTER_USE_BOTH);