6

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

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 to remove the element in where I have the userid 4 and then save it all back into $my_array like this:

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

     [2] => Array
        (
            [username] => John
            [userid] => 3
        )

)

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

Thanks in advance :-)

Mansa
  • 2,277
  • 10
  • 37
  • 67
  • 2
    Use `foreach ($key => $value)` to iterate over the array. Test for the condition you want on `$value` and when it matches, `unset($my_array[$key])`. There is no need to "save" anything. I suggest reading the examples on the manual. – Jon Mar 21 '13 at 22:51

1 Answers1

9

Try this:

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

    if ($value["userid"] == 4) { unset($array[$key]); }

}
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308