RecursiveArrayIterator - PHP Online
Form of PHP Sandbox
Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php
in the places where the PHP script should be executed.
Result of php executing
Full code of RecursiveArrayIterator.php
- <?php
- $hit = [
- '_source' => [
- 'account' => [
- 'id' => 123,
- 'first_name' => 'Foo',
- 'last_name' => 'Bar',
- 'mail' => 'for@bar',
- 'name' => 'Foo Bar',
- ],
- ],
- ];
- $accountFieldsToRemove = [
- 'first_name',
- 'last_name',
- 'mail',
- 'name',
- ];
- $iterator = new RecursiveArrayIterator($hit);
- traverseArray($accountFieldsToRemove, $iterator);
- function traverseArray(array $accountFieldsToRemove, RecursiveArrayIterator $iterator)
- {
- while ($iterator->valid()) {
- if ($iterator->hasChildren()) {
- traverseArray($accountFieldsToRemove, $iterator->getChildren());
- } else {
- if (in_array($iterator->key(), $accountFieldsToRemove)) {
- echo "#{$iterator->key()} - {$iterator->current()}\r\n";
- //$iterator->offsetUnset($iterator->key());
- }
- }
- $iterator->next();
- }
- }