1
Array
(
    [15,  BBC company] => Array
        (
            [147, IT Dep] => 85
            [150, HR Dep] => Array
                (
                    [58] => employee 1
                    [71] => employee 2
                )

        )

)

I would like to delete [58] => employee 1 from array The following is my code, however, it doesn't work.

Can anyone help me? Many thanks!

foreach($arr as $key=>$value){

    foreach ($value as $subKey=>$subVal){

        foreach($subVal as $k=>$v){

            if($k==58){

                echo $k;

                unset($subVal[$k]);

            }
        }

    }

}

Expected result

Array
(
    [15,  BBC company] => Array
        (
            [147, IT Dep] => 85
            [150, HR Dep] => Array
                (
                    [71] => employee 2
                )

        )

)
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Acubi
  • 2,793
  • 11
  • 41
  • 54

4 Answers4

1

Delete an element from an array

unset($myArray[15][150][58]);

where 15 = BBC Company
      150 = HR Dept
      58 = some employee you want fired

you already know where to go until you reach the third dimension. you just have to fill it in.

also, what you are currently doing is you are unsetting every 58th-indexed value in the 3rd dimension of your array - which is wrong by the way. let's say you have an employee who happens to have index 58 in the IT Dept array. You'd also delete him too with your current code.

Community
  • 1
  • 1
Joseph
  • 117,725
  • 30
  • 181
  • 234
1

the code must be as follows:

foreach($arr as $key=>&$value){
    foreach ($value as $subKey=>&$subVal){    
        foreach($subVal as $k=>&$v){
            if($k==58){
                echo $k;
                unset($subVal[$k]);
            }
        }
    }
}

because foreach creates a copy of the array, and you are unsetting an element of this copy. in the code above the array is passed as a reference

k102
  • 7,861
  • 7
  • 49
  • 69
1

The variables provided by foreach loops are by value, not reference, so you can't modify the original, and forcing using a reference in a foreach with the & is not reccomended. You're much better off using the variable directly with the keys the foreach provides, rather than the variables it provides.

foreach($arr as $key=>$value){

    foreach ($arr[$key] as $subKey=>$subVal){

        foreach($arr[$key][$subKey] as $k=>$v){

            if($k==58){

                echo $k;

                unset($arr[$key][$subKey][$k]);

            }
        }
    }
}

Completely untested.

See here: http://php.net/manual/en/control-structures.foreach.php

Paystey
  • 3,287
  • 2
  • 18
  • 32
1
$arr = Array(
       '15,  BBC company' => Array(
                            '147, IT Dep' => 85,
                            '150, HR Dep' => Array(
                                             '58' => 'employee 1',
                                             '71' => 'employee 2'
                                             )
                            )
     );

foreach($arr as $key=>$value){
    foreach ($value as $subKey=>$subVal){
        if(is_array($subVal)):
          foreach($subVal as $k=>$v) {
            if($k==58){
                echo $k;
                echo '<br />';
                print_r($value[$subKey][$k]);
                echo '<br />';
                unset($arr[$key][$subKey][$k]);
            }
          }
        endif;
    }
}
print_r($arr);
Swatantra Kumar
  • 1,324
  • 5
  • 24
  • 32