0

Say, I have an array with this structure:

Array
(
    [0] => Array
        (
            [0] => data
            [1] => data
            [2] => data
            [3] => Array
                (
                    [0] => data
                    [thatbastard] => Array
                        (
                            [0] => Array
                                (
                                    [0] => data
                                    [1] => data
                                    [2] => data
                                    [3] => data
                                )

                            [1] => Array
                                (
                                    [0] => data
                                    [1] => data
                                    [2] => data
                                    [3] => data
                                )

                            [2] => Array
                                (
                                    [0] => data
                                    [1] => data
                                    [2] => data
                                    [3] => data
                                )
                        )

                    [2] => data
                )

            [4] => Array
                (
                    [0] => data
                    [thatbastard] => Array
                        (
                            [0] => Array
                                (
                                    [0] => data
                                    [1] => data
                                    [2] => data
                                    [3] => data
                                )

                            [1] => Array
                                (
                                    [0] => data
                                    [1] => data
                                    [2] => data
                                    [3] => data
                                )

                            [2] => Array
                                (
                                    [0] => data
                                    [1] => data
                                    [2] => data
                                    [3] => data
                                )
                        )

                    [2] => data
                )
        )

    [1] => Array(similar to [0])
)

I want to get rid of [thatbastard] all over the array, so my array will look like this:

Array
(
    [0] => Array
        (
            [0] => data
            [1] => data
            [2] => data
            [3] => Array
                (
                    [0] => data
                    [2] => data
                )

            [4] => Array
                (
                    [0] => data
                    [2] => data
                )
        )

    [1] => Array(similar to [0])
)

I've searched widely for hints, but most of them use key_by_value search. In my case I don't care about value, I want to get rid of data by specific key.

I've tried solution based on this topic with recursive function:

 public function filterList($arr, $remove_key)
    {
        foreach ($arr as $key => $value)
        {
            if (is_array($value)) {
                $filtered[$key] = $this->filterList($value, $remove_key);
            }
            else
            {
                if ($key != $remove_key)
                {
                    $filtered[$key] = $value;
                }
            }
        }
        return $filtered;
    }

but it gives the same output as original array.
I've tried this solution, but apparently my array has too many depth levels, and I can't figure out how to improve code.
The problem is I want to write some multitask function that can work with different arrays, not only example.

2 Answers 2

3

You can use the array_filter_recursive function from the PHP array_filter documentation. That will work for any multi-dimensional arrays.

<?php


$sweet = array('a' => 'Apple', 'b' => 'Banana');
$fruits = array('sweet' => $sweet, 'sour' => 'Lemon');

function array_filter_recursive($input, $callback = null) 
{ 
  foreach ($input as &$value) 
  { 
    if (is_array($value)) 
    { 
      $value = array_filter_recursive($value, $callback); 
    } 
  }
  return array_filter($input, $callback, ARRAY_FILTER_USE_BOTH); 
} 


$result = array_filter_recursive($fruits, function ($value, $key) {
  return ($key != 'b');
});

var_dump($result);
Sign up to request clarification or add additional context in comments.

2 Comments

Could you please make a hint what $i valiable is for?
Oh, now I see. Thanks! Apparently working with filter + callback is tricky for me. I've tried to make unnecessary key as variable (by adding parameter for callback noname function and array_filter_recursive () and got either null or original array.
1

This is a way to do it. Probably not the best way, but it'll get you the results you want.

foreach($array1 as $key1 => $array2) {

    foreach($array2 as $key2 => $array3) {

        foreach($array3 as $key3 => $data) {

            if($key3 == 'thatbastard') unset($array1[$key1][$key2][$key3]);

        }

    }

}

It's worth noting that this relies on the third tier of the array being the only tier with a key with that name. If the key can be at multiple levels then you'll want to adjust this code to run the unset command at those different levels as well.

To get this to work for you, simply change $array1 to the name of your array and leave all of the other variables as they are.

3 Comments

Yeah, it's my first thought too, but I want to write more independent solution which can work with differents arrays as well (sorry for not specified...). Thanks for answer though!
So you essentially want a function that removes a specific key from a multi-dimensional array at any level?
In a nutshell, yes. I've tried different solutions, but they are okay only for array with depth level = 2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.