PHP-从多维数组中删除重复的值

cr92

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

数组示例:

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

我想删除重复的内容name所以我只想每天保留一个主题。

到目前为止,我的代码:

$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]);
    }

}

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

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

任何人都可以帮助我,我怎么可以删除同一类nameTuesday

凯文

如果要在每个value批次中使用保留第一组唯一值name,则只需为此创建一个临时容器。如果您已经推它,则不能正确处理任何事情,聚会后,使用覆盖批次foreach&参考:

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
}

样本输出

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章