开发者

How to remove duplicate keys in array

开发者 https://www.devze.com 2023-04-08 21:23 出处:网络
I have an array called $myarray - id position status name 4234john 3453mike 4230john 7252sam etc. i need to filter array by \"id\" and when similar records found i need to evaluate by \"statu

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

精彩评论

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