Functional approach:
$array = array_filter($array, function($key) {
return strpos($key, 'foo-') === 0;
}, ARRAY_FILTER_USE_KEY);
Procedural approach:
$only_foo = array();
foreach ($array as $key => $value) {
if (strpos($key, 'foo-') === 0) {
$only_foo[$key] = $value;
}
}
Procedural approach using objects:
$i = new ArrayIterator($array);
$only_foo = array();
while ($i->valid()) {
if (strpos($i->key(), 'foo-') === 0) {
$only_foo[$i->key()] = $i->current();
}
$i->next();
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…