0

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?

2
  • I'm confused about your array structure. If I wanted mike's name, would I look at $myarray[1]['name'] (row #1) or $myarray[3]['name'] (id=3) Commented Sep 26, 2011 at 5:12
  • Note: There are no duplicate keys in PHP arrays. Only duplicate values. Just as this question is a duplicate of many others: stackoverflow.com/questions/5960190/… Commented Sep 26, 2011 at 5:13

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

array_filter()

This might be of your use

2 Comments

array_filter doesn't compare two elements in an array.. It only looks at one.
The callback function is only passed one element of the array.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.