How can I remove duplicate values from a multi-dimensional array in PHP?

如何从PHP中的多维数组中删除重复值?

Example array:

   Array
(
    [choice] => Array
        (
            [0] => Array
                (
                    [day] => Monday
                    [value] => Array
                        (
                            [0] => Array
                                (
                                    [name] => BI
                                    [time] => 10:00 
                                    [location] =>  B123
                                )
                            [1] => Array
                                (
                                    [name] => BI
                                    [time] => 11:00 
                                    [location] =>  A123
                                )
                        )
                )

            [1] => Array
                (
                    [day] => Tuesday
                    [value] => Array
                        (
                            [0] => Array
                                (
                                    [name] => BI
                                    [time] => 10:00 
                                    [location] =>  B123
                                )
                            [1] => Array
                                (
                                    [name] => BI
                                    [time] => 11:00 
                                    [location] =>  A123
                                )
                        )
                    )
        )
)

I'd like to remove those with duplicate name. So i only want to keep one subject each day.

我想删除名称重复的那些。所以我只想每天保留一个主题。

my code so far:

我的代码到目前为止:

$taken = array();
foreach($subject_list['choice'][0]["value"] as $key =>$item )
{ 
    if(!in_array($item['name'], $taken)) 
    {
        $taken[] = $item['name'];
    }else 
    {
        unset($flight_list['choice'][0]["value"][$key]);
    }

}

OUTPUT of the code above (which is obviously wrong):

上面代码的输出(显然是错误的):

Array
(
    [choice] => Array
        (
            [0] => Array
                (
                    [day] => Monday
                    [value] => Array
                        (
                            [0] => Array
                                (
                                    [name] => BI
                                    [time] => 10:00 
                                    [location] =>  B123
                                )
                        )
                )

            [1] => Array
                (
                    [day] => Tuesday
                    [value] => Array
                        (
                            [0] => Array
                                (
                                    [name] => BI
                                    [time] => 10:00 
                                    [location] =>  B123
                                )
                            [1] => Array
                                (
                                    [name] => BI
                                    [time] => 11:00 
                                    [location] =>  A123
                                )
                        )
                    )
        )
)

Anyone can help me how can i remove same class name at Tuesday.

任何人都可以帮助我如何在星期二删除相同的班级名称。

3 个解决方案

#1


If you want to keep the first set of unique values in each value batch in terms of name, then just create a temporary container for that. If you already have pushed it, then don't process anything, after the gathering, overwrite the batch using foreach with & reference:

如果要根据名称在每个值批处理中保留第一组唯一值,则只需为其创建一个临时容器。如果你已经推了它,那么不要处理任何东西,在收集之后,使用foreach和&reference覆盖批处理:

foreach($subject_list['choice'] as &$items) {
    $temp = array(); // temporary container for current iteration
    foreach($items['value'] as $value) {
        if(!isset($temp[$value['name']])) { // if its new
            $temp[$value['name']] = $value; // push the batch using the key name
        }
    }
    $items['value'] = $temp; // apply unique value in the end of this batch
}

Sample Output

更多相关文章

  1. 如果我对名称中方括号的输入使用jQuery serialize(),为什么会出现
  2. 无法在Yii中更改项目的文件夹名称
  3. MySQL命名约定,字段名称是否应包含表名?
  4. 如何创建一个新表,该表根据名称获取其他表中列的平均值
  5. 如何知道php脚本中的名称是指mysql表还是mysql视图
  6. 'mysql'不是内部或外部命令,也不是可运行的程序或批处理文件.
  7. 如何使用mysql在php文件夹中快速更改图像名称?
  8. 在批处理文件中使用JSMin
  9. 具有相同名称的Mutiple按钮显示不同的div。

随机推荐

  1. Unity 与 Android (Android Studio)的交
  2. android中android:wight详解
  3. 横屏和竖屏切换不用销毁重新建立android:
  4. android 9.0系统重启的代码详解
  5. Android内核源码阅读---ashmem.c
  6. Android的一些常用命令提示符(cmd)指令
  7. Android中使用SAX方式解析XML文件
  8. React Native Android环境配置 第一个实
  9. Android(安卓)子fragment中获取父fragmen
  10. 【Mark】Android TextView属性详解