0

I have an array $rows that looks like the following:

Array
(
    [0] => Array
        (
            [type] => post
            [object_id] => 1
            [language_id] => 1
            [score] => 2
        )

    [1] => Array
        (
            [type] => category
            [object_id] => 46
            [language_id] => 1
            [score] => 1
        )

    [2] => Array
        (
            [type] => product
            [object_id] => 50
            [language_id] => 1
            [score] => 1
        )

    [3] => Array
        (
            [type] => category
            [object_id] => 59
            [language_id] => 1
            [score] => 1
        )
    [4] => Array
        (
            [type] => post
            [object_id] => 1
            [language_id] => 1
            [score] => 1
        )
)

I need to loop through this array and when a duplicate type and object_id matches, increment the score and delete the duplicate array. I can't seem to find a way to do this.

Using the example above, key 0 has type post and object_id 1, and so does key 4. Key 0 score should be incremented to itself + key 4 score and key 4 should be deleted.

My end result should look like this:

Array
(
    [0] => Array
        (
            [type] => post
            [object_id] => 1
            [language_id] => 1
            [score] => 3
        )

    [1] => Array
        (
            [type] => category
            [object_id] => 46
            [language_id] => 1
            [score] => 1
        )

    [2] => Array
        (
            [type] => product
            [object_id] => 50
            [language_id] => 1
            [score] => 1
        )

    [3] => Array
        (
            [type] => category
            [object_id] => 59
            [language_id] => 1
            [score] => 1
        )
)
4
  • 1
    Post your attempts along with expected output Commented Aug 12, 2015 at 13:35
  • 1
    Just rewrite a new function to merge your array with itself. Commented Aug 12, 2015 at 14:28
  • Try array_values(array_unique($array)) Commented Aug 12, 2015 at 14:32
  • How is this going to increment the score of the existing type/object_id array? Commented Aug 12, 2015 at 15:19

1 Answer 1

1

You can achieve that simply using foreach as

$result = [];

foreach ($arr as $key => $value) {
    $hash = $value['type'];
    $hash1 = $value['object_id'];
    if (isset($result[$hash.$hash1]['type']) && isset($result[$hash.$hash1]['object_id'])) {
        $result[$hash.$hash1]['type'] = $value['type'];
        $result[$hash.$hash1]['object_id'] = $value['object_id'];
        $result[$hash.$hash1]['language_id'] = $value['language_id'];
        $result[$hash.$hash1]['score'] += $value['score'];
    } else {
        $result[$hash.$hash1]['type'] = $value['type'];
        $result[$hash.$hash1]['object_id'] = $value['object_id'];
        $result[$hash.$hash1]['language_id'] = $value['language_id'];
        $result[$hash.$hash1]['score'] = $value['score'];
    }
}

print_r(array_values($result));

Demo

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

1 Comment

Perfect, I had to do an additional array_multisort and adjust my initial count for the new items in the array, but overall, perfect. Thanks so much!

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.