开发者

How to remove duplicate keys in array

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 tha开发者_开发问答t's 0 or less than "status" in matching row. Is there some way to do it fast like some function?


You could use array_multisort(..) to sort by id ASC then status DESC. Then you could walk through the sorted array and delete rows where the id has been seen before.


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;
}


array_filter()

This might be of your use

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜