recursiveRemoval - 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 recursiveRemoval.php
- <?php
- $hits = [
- '_source' => [
- 'account' => [
- 'id' => 123,
- 'first_name' => 'Foo',
- 'last_name' => 'Bar',
- 'mail' => 'for@bar',
- 'name' => 'Foo Bar',
- ],
- ],
- '_source2' => [
- 'account2' => [
- 'id' => 456,
- 'first_name' => 'Foo2',
- 'last_name' => 'Bar2',
- 'mail' => 'for@bar2',
- 'name' => 'Foo Bar2',
- ],
- ],
- '_source' => [
- 'parent' => [
- 'authors' => [
- [
- 'id' => 777,
- 'first_name' => 'Foo',
- 'last_name' => 'Bar',
- 'mail' => 'for@bar',
- 'name' => 'Foo Bar',
- ],
- ],
- ],
- ],
- 'fields' => [
- 'in_array_strict_mode' => [
- 0 => 21,
- ],
- ],
- ];
- $accountFieldsToRemove = [
- 'first_name',
- 'last_name',
- 'mail',
- 'name',
- ];
- recursiveRemoval($hits, $accountFieldsToRemove);
- print_r($hits);
- function recursiveRemoval(array &$hits, array $accountFieldsToRemove)
- {
- foreach ($hits as $key => &$hitElement) {
- if (is_array($hitElement)) {
- recursiveRemoval($hitElement, $accountFieldsToRemove);
- } else {
- if (in_array($key, $accountFieldsToRemove, true)) {
- unset($hits[$key]);
- }
- }
- }
- }
File Description
- recursiveRemoval
- PHP Code
- 29 Sep-2022
- 1.46 Kb
You can Share it:
Latest PHP Pastes