Last active
July 17, 2016 09:19
-
-
Save slifin/c999e263bcbeb5ed347f50e691fcb0c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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