Quantcast
Viewing latest article 32
Browse Latest Browse All 42

Answer by Ja͢ck for Search for part of an key in array - PHP

You can use a negative filter (with strpos) instead:

foreach ($array as $key => $value) {    if (strpos($key, 'array_') !== 0) {        unset($array[$key]);    }}

Demo

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);

Viewing latest article 32
Browse Latest Browse All 42

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>