remove element in multidimensional array and save [closed]

风格不统一 提交于 2020-02-23 10:23:14

问题


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 :-)


回答1:


Try this:

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

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

}


来源:https://stackoverflow.com/questions/15559651/remove-element-in-multidimensional-array-and-save

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!