Skip to content

Instantly share code, notes, and snippets.

@slifin
Last active July 17, 2016 09:19
Show Gist options
  • Select an option

  • Save slifin/c999e263bcbeb5ed347f50e691fcb0c4 to your computer and use it in GitHub Desktop.

Select an option

Save slifin/c999e263bcbeb5ed347f50e691fcb0c4 to your computer and use it in GitHub Desktop.
<?php
$hot_mess =
array(
5 => 5,
3 => array(
12,
12 => array(
'name' => 1
),
32 => array(
'name' => 4
)
)
);
function filter_array($array, $callback, $keep_index = FALSE) {
return iterator_to_array(new CallbackFilterIterator(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)), $callback), $keep_index);
}
$test = filter_array($hot_mess, function($value, $key, $iterator) {
return $key === 'name' || $iterator->getDepth() == 1;
});
var_dump($test);
var_dump($hot_mess);
$hot_mess =
array(
5 => 5,
3 => array(
12,
12 => array(
'name' => 1
),
32 => array(
'name' => 4
)
)
);
function iterate_array(array $array, $flag = RecursiveIteratorIterator::LEAVES_ONLY) {
return new RecursiveIteratorIterator(new RecursiveArrayIterator($array), $flag);
}
function filter_iterator($iterator, $callback) {
return new CallbackFilterIterator($iterator, $callback);
}
$iterator = filter_iterator(iterate_array($hot_mess), function($value, $key, $iterator) {
return $key === 'name' || $iterator->getDepth() == 1;
});
$test = iterator_to_array($iterator, FALSE);
var_dump($test);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment