Delete item in multidimensional array and save

I am trying to figure out how to remove one core element and all its siblings and subsequently save the array.

Here is what I got:

$my_array = Array ( [0] => Array ( [username] => Pete [userid] => 2 ) [1] => Array ( [username] => James [userid] => 4 ) [2] => Array ( [username] => John [userid] => 3 ) ) 

Now, what I want to do is remove the element in which I have userid 4, and then save it back to $ my_array as follows:

 $my_array = Array ( [0] => Array ( [username] => Pete [userid] => 2 ) [2] => Array ( [username] => John [userid] => 3 ) ) 

Can this be done? and if so ... How ???

Thank you in advance: -)

+6
source share
1 answer

Try the following:

 foreach ($array as $key => $value) { if ($value["userid"] == 4) { unset($array[$key]); } } 
+9
source

All Articles