mercredi 6 mars 2019

Php Array_filter to unset key values from array of arrays

I don't want to use for each loop in my code and want unset array elements from my nested array of arrays.

I want to perform recursive array filter and i am not sure about how to do that efficiently.

Below is the original array.

[
  [a] => 3
  [b] => 0
  [c] => [
    [
      [1] => aa
      [2] => 1
      [3] => [
                [
                  [a1] => 6
                  [a2] => 5781
                  [a3] =>
                      [
                        [1] => 0
                        [2] => 19550
                        [3] => 5781
                      ]
                ]
                [
                  [a1] => 1
                  [a2] => 5781
                  [a3] =>[
                          [1] => 0
                          [2] => 19550
                          [3] => 5781
                      ]
                ]
              ]
    ]
    [
      [1] => aa
      [2] => 1
      [3] => [
                [
                  [a1] => 6
                  [a2] => 5781
                  [a3] =>
                      [
                        [1] => 0
                        [2] => 19550
                        [3] => 5781
                      ]
                ]
                [
                  [a1] => 1
                  [a2] => 5781
                  [a3] =>[
                          [1] => 0
                          [2] => 19550
                          [3] => 5781
                      ]
                ]
              ]
    ]
  ]
]

Expected array

[
  [a] => 3
  [c] => [
    [
      [1] => aa
      [3] => [
                [
                  [a1] => 6
                  [a3] =>
                      [
                        [2] => 19550
                        [3] => 5781
                      ]
                ]
                [
                  [a1] => 1
                  [a3] =>[
                          [2] => 19550
                          [3] => 5781
                      ]
                ]
              ]
    ]
    [
      [1] => aa
      [3] => [
                [
                  [a1] => 6
                  [a3] =>
                      [
                        [2] => 19550
                        [3] => 5781
                      ]
                ]
                [
                  [a1] => 1
                  [a3] =>[
                          [2] => 19550
                          [3] => 5781
                      ]
                ]
              ]
    ]
  ]
]

As you can see in my expected array from every nested arrays some key value pairs have been removed. I need to filter the array based on key name. That is my key names are fixed and that needs to be removed from every children arrays inside.

Any helps would be appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire