如何删除数组中的重复键 [英] How to remove duplicate keys in array

查看:192
本文介绍了如何删除数组中的重复键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 $ myarray -

  id位置的数组状态名称

4 23 4 john

3 45 3 mike

4 23 0 john

7 25 2山姆

等。

我需要过滤数组由ID和类似的记录发现时,我需要通过状态键来评估匹配行中删除0或小于状态的数字。有没有办法做到这一点像一些函数?

解决方案

这将删除所有 status = 0 。如果你想保留这些行,当没有其他匹配 id 时,移除 $ value ['status']> 0&< / code>

  $ cleanarray = array(); 
foreach($ myarray as $ value){
if(
$ value ['status']> 0&&(
!array_key_exists($ value ['id '],$ cleanarray)||
$ value ['status']> $ cleanarray [$ value ['id']] ['status']

)$ new_cleanarray [ $ value ['id']] = $ value;
}


I have an array called $myarray -

id position status name

4    23        4   john

3    45        3   mike

4    23        0   john

7    25        2   sam

etc.

i need to filter array by "id" and when similar records found i need to evaluate by "status"key to remove number that's 0 or less than "status" in matching row. Is there some way to do it fast like some function?

解决方案

This removes all occurences of status=0. If u want to keep these rows, when there is no other matching id, remove the $value['status'] > 0 &&

$cleanarray = array();
foreach ($myarray as $value) {
    if (
        $value['status'] > 0 && (
            !array_key_exists($value['id'], $cleanarray) ||
            $value['status'] > $cleanarray[$value['id']]['status']
        )
    ) $new_cleanarray[$value['id']] = $value;
}

这篇关于如何删除数组中的重复键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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