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.



Your result can be seen below.

Result of php executing





Full code of recursiveRemoval.php

  1. <?php
  2.  
  3. $hits = [
  4.     '_source'  => [
  5.         'account' => [
  6.             'id'         => 123,
  7.             'first_name' => 'Foo',
  8.             'last_name'  => 'Bar',
  9.             'mail'       => '[email protected]',
  10.             'name'       => 'Foo Bar',
  11.         ],
  12.     ],
  13.     'parent' => [
  14.         'authors' => [
  15.             [
  16.                 'id'         => 777,
  17.                 'first_name' => 'Foo',
  18.                 'last_name'  => 'Bar',
  19.                 'mail'       => '[email protected]',
  20.                 'name'       => 'Foo Bar',
  21.             ],
  22.         ],
  23.  
  24.     ],
  25.     'fields'   => [
  26.         'in_array_strict_mode' => [
  27.             0 => 21,
  28.         ],
  29.     ],
  30. ];
  31.  
  32. $accountFieldsToRemove = [
  33.     'first_name',
  34.     'last_name',
  35.     'mail',
  36.     'name',
  37. ];
  38.  
  39. recursiveRemoval($hits, $accountFieldsToRemove);
  40. print_r($hits);
  41.  
  42. function recursiveRemoval(array &$hits, array $accountFieldsToRemove)
  43. {
  44.     foreach ($hits as $key => &$hitElement) {
  45.         if (is_array($hitElement)) {
  46.             recursiveRemoval($hitElement, $accountFieldsToRemove);
  47.         } else {
  48.             if (in_array($key, $accountFieldsToRemove, true)) {
  49.                 unset($hits[$key]);
  50.             }
  51.         }
  52.     }
  53. }
File Description
  • recursiveRemoval
  • PHP Code
  • 29 Sep-2022
  • 1.15 Kb
You can Share it: