如何在PHP中的多维数组中执行条件删除? [英] How to perform conditional delete in multidimensional array in PHP?

查看:53
本文介绍了如何在PHP中的多维数组中执行条件删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要您的帮助,问题描述已给出

I need your help the problem description is given

数组如下

给定的数组是示例.主数组包含许多条目,如下所示,其中用户名可以不同.

The given array is the sample. The main array contains many entries as follow where username can differ.

情况1:

$my_array = Array
(
0 => Array
    (
        'username' => Pete,
        'userid' => 1,
        'option'=>no
    ),

1 => Array
    (
        'username' => Pete,
        'userid' => 2,
        'option'=>yes
    ),

2 => Array
    (
        'username' => John,
        'userid' => 1,
        'option'=>no
    )
3 => Array
    (
        'username' => John,
        'userid' => 1,
        'option'=>yes
    )

) ;

情况2:

$my_array = Array
(
0 => Array
    (
        'username' => Pete,
        'userid' => 2,
        'option'=>yes
    ),

1 => Array
    (
        'username' => Pete,
        'userid' => 1,
        'option'=>no
    ),

2 => Array
    (
        'username' => John,
        'userid' => 1,
        'option'=>no
    )
3 => Array
    (
        'username' => John,
        'userid' => 1,
        'option'=>yes
    )

) ;

我要删除'username'=> Pete和'Option'=> no

I want to delete the element where 'username'=>Pete and 'Option'=>no

所以输出应该看起来像

Array
(
[0] => Array
    (
        [username] => Pete
        [userid] => 2
        [option] => yes
    )
[2] => Array
    (
        [username] => John
        [userid] => 1
        [option] => yes
    )
)

子数组的所有元素都可以相同,但选项字段为是或否.

All element of sub array can be same but option field either yes or no.

请帮助我

非常感谢您

推荐答案

使用此代码,您可以实现以下目标:

With this code you can achieve that:

foreach($my_array as $key => $value){
    if($value['username'] == 'Pete' && $value['option'] == 'yes'){
        unset($my_array[$key]);
    }
}

这篇关于如何在PHP中的多维数组中执行条件删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆