1

I have a php array that looks like this:

Array(
[3086] => Array
    (
        [id] => 3086
        [note] => Make text larger
        [revision] => 1
        [noteParentId] => 1706
    )

[3087] => Array
    (
        [id] => 3087
        [note] => Make text larger
        [revision] => 2
        [noteParentId] => 1706
    )

[3085] => Array
    (
        [id] => 3085
        [note] => Enlarge this image
        [revision] => 1
        [noteParentId] => 1705
    )

[3084] => Array
    (
        [id] => 3086
        [note] => Another test note
        [revision] => 1
        [noteParentId] => 1704
    )

)

How can I filter it in such a way that if the [noteParentId] has the same value (as seen in [3086] and [3087]), then remove the one with the lower [revision] value from the array?

0

3 Answers 3

2

You should sort the array

function mysort($a, $b){
    if ($a['revision'] >= $b['revision'])
        return 1;
    return -1;
}

and then store the matching values in another array

$arrResult = array();
usort($arrTest, "mysort");
foreach ($arrTest as $key=>$value){
    if (!isset($arrResult[$value['noteParentId']]))
        $arrResult[$value['noteParentId']] = array($key=>$value);
}

Now you will need to sanitize $arrResult...

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

Comments

1

This answer will require a little more code than the previous answers, but I think it's a more efficient solution for the following reasons:

  • It will always be a O(n) solution for you
  • It keeps the same data structure you expected
  • It won't require you to merge multiple filtered result sets. and merges of data.

////

function filterOldRevisions($tasks) {

    $revisionHash = array();

    foreach ($tasks as $taskId => $task) {
        if (isset($revisionHash[$task['noteParentId']])) {
            $currentMaxRevision = $revisionHash[$task['noteParentId']];

            if ($task['revision'] > $revisionHash[$task['noteParentId']]) {
                //store the max revision for the parent in the hash
                $previousMaxId = $revisionHash[$task['noteParentId']]['id'];
                $revisionHash[$task['parentId']]  = $task;

                //remove the previous max revision
                unset($tasks[$previousMaxId]);
            } else {
                //remove lower revision
                unset($tasks[$taskId]);
            }
        } else {
            //always store the first task
            $revisionHash[$task['noteParentId']]  = $task;
        }
    }

    return $tasks;
}

Comments

1

You can use the array_filter function http://php.net/manual/en/function.array-filter.php

example:

$parentId = 1706;
$filtered = array_filter($data, function($item) use ($parentId) { 
   return $item['noteParentId'] === $parentId; 
});

or if you modify the sql query, you can use group by and filter by count(parent_id) > 1

example:

SELECT noteParentId, count(*) FROM someTable GROUP BY noteParentId WHERE count(*) > 1;

2 Comments

Both of these examples are wrong. The first one will only return arrays where noteParentId is the same as what you want, and the second will only return rows where there are more than one revision.
Thank you for pointing that out Robbie. If rows have a matching noteParentId, then I want to keep the row with the highest revision out of those rows, as well as keep all of the other entries with different noteParentId's.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.