Home > Back-end >  PHP Multidimensional Array - Unset based on Element Value
PHP Multidimensional Array - Unset based on Element Value

Time:11-17

I have a query regarding unsetting arrays within a multidimensional array where element = 0 for example.

In the below array example

    [Something] => Array
(
    [Something1] => Array
    (
        [0] => Array
        (
            [@attributes] => Array
                (
                    [DataID] => Data
                    [DataID] => Data
                    [DataID] => Data
                    [DataID] => Data
                )

            [Something1.1] => Array
            (
                [Something1.1.1] => Array
                (
                    [0] => Array
                    (
                        [DataID] => Data
                        [Something] => 0
                        [DataID] => Data
                        [DataID] => Data
                        [DataID] => Data
                        [DataID] => Data
                    )
                    [1] => Array
                    (
                        [DataID] => Data
                        [Something] => 4
                        [DataID] => Data
                        [DataID] => Data
                        [DataID] => Data
                        [DataID] => Data

                    )
                    [2] => Array
                    (
                        [DataID] => Data
                        [Something] => 3
                        [DataID] => Data
                        [DataID] => Data
                        [DataID] => Data
                        [DataID] => Data

                    )
                )
            )
            [Something1.2] => Array
            (
                [Something.1.2.1] => Array
                    (
                        [0] => Array
                    (
                        [DataID] => Data
                        [Something] => 0
                        [DataID] => Data
                        [DataID] => Data
                        [DataID] => Data
                        [DataID] => Data
                    )
                    [1] => Array
                    (
                        [DataID] => Data
                        [Something] => 4
                        [DataID] => Data
                        [DataID] => Data
                        [DataID] => Data
                        [DataID] => Data

                    )
                    [2] => Array
                    (
                        [DataID] => Data
                        [Something] => 2
                        [DataID] => Data
                        [DataID] => Data
                        [DataID] => Data
                        [DataID] => Data
                    )
                )
            )
        )
    )
)

In my code for example, I have this though it doesn't seem to unset the array where Somethings element value is 0

foreach ($Somethings as $key2 => $Something) 
                        {
                            $value = 0;
                            if ($Somethings['Something - Element ID'] == $value) 
                            {
                                unset($Somethings[$key2]);                      
                            }       
                            $Something = array_values( $Something);
                            $Thingswithout0 = true;                         
                        }

I'd like to for example remove Something1.1.1 [0]

[Something1.1] => Array ( [Something1.1.1] => Array ( [0] => Array (

If Something within => 0

CodePudding user response:

I have tried the following in a foreach loop and debugging with a var_export, I can see the array is removed exactly as required based on specified element value. Taking on the advice from Markus, array_values is no more as "evil" :)

    foreach ($av as $k => $a) 
    {
        $value= 0;                          
        if ($a['ElementID'] == $value) 
        {
            unset($a);                                                                  
        }
    }

Is this a safe and recommended approach?

CodePudding user response:

If you only have a foreach loop, your error is that you are using array_values() inside the loop and passing inside the function the variable $Something instead of $Somethings.

But if you want to go through the whole array to find those "0" and delete their parents, you should use several loops and then you can delete the correct value from the multiarray.

So assuming that the multi-array in the example is stored in a variable named $array:

// Go through each of the arrays saving the KEY at each foreach so as not to lose the thread.
foreach($array["Something"]["Something1"] as $key => $value) {
    foreach($value as $key1 => $value1){
        foreach($value1 as $key2 =>  $value2){
            // Because in this level we have values and arrays, we must loop only the arrays
            if(is_array($value2)) {
                foreach($value2 as $key3 =>  $value3) {
                    // Once looping the desired arrays, compare value
                    if($value3['Something'] == 0){
                        // Then remove the value you want by calling the original variable and tracing the path to the desired value
                        unset($array["Something"]["Something1"][$key][$key1][$key2][$key3]);
                    }
                }
            }
        }
    }
}

// Out of the loop, reindex the array
$array = array_values($array);
  • Related