0

I want to merge two same keys in an array and get the sum of the values. I want the same structure as it is now.Because this data needs to be converted to JSON.

This is what i get now.

{ "data": [{ "count_of_invites": 5, "user": "Rajesh", "id": "53" }, { "count_of_invites": 9, "user": "Student", "id": "45" }, { "count_of_invites": 4, "user": "Student", "id": "45" } ] }

As you can see the id 45 are repeated.As i want the result as,

Expected output { "data": [{ "count_of_invites": 5, "user": "Rajesh", "id": "53" }, { "count_of_invites": 13, "user": "Student", "id": "45" } ] }

As you can see the duplicate entry should be removed as well as the count_of_invites of duplicate entry should be added.

5

3 Answers 3

1
<?php
$data = [
    [
        'id' => 2,
        'name' => 'Paul',
        'count' => 4
    ],
    [
        'id' => 3,
        'name' => 'Peter',
        'count' => 5
    ],
    [
        'id' => 3,
        'name' => 'Peter',
        'count' => 7
    ]
];

foreach($data as $array)
    $counts[$array['id']][] = $array['count'];

$counts = array_map('array_sum', $counts);
foreach($data as $k => $array)
    $data[$k]['count'] = $counts[$array['id']];

$data = array_unique($data, SORT_REGULAR);
print json_encode($data, JSON_PRETTY_PRINT);

Output:

[
    {
        "id": 2,
        "name": "Paul",
        "count": 4
    },
    {
        "id": 3,
        "name": "Peter",
        "count": 12
    }
]
Sign up to request clarification or add additional context in comments.

Comments

0

You can achieve it this way:

$ids = array();
$output = array();

foreach ($input as $value) {
    if (!isset($ids[$value["id"]])) {
        $ids[$value["id"]]=$count($output);
        $output[]=$value;
    } else {
        $output[$ids[$value["id"]]]["count_of_invites"] = $value["count_of_invites"];
        $output[$ids[$value["id"]]]["user"] = $value["user"];
    }
}

6 Comments

[ { count_of_invites: 5, user: "Rajesh", id: "53" }, { count_of_invites: 4, user: "Student", id: "45", student: null } ]This is what i get after encoding it to json.As the value doent sum up
@KiranJB I have edited my answer, please, check it out. The mistake was that in the else I have used a student field instead of the user field.
The count_of_invites shows the last data only.not the sum of all the value.my problem is to get the sum of values which has same key and in single occurence
@Kiran can you please update your question then as I asked an hour ago. I'm not too keen or typing your whole array in manually.
@Lajos Arpad .There were some editing to your mentioned code .But I've solve the problem .Thank you Arpad .It was a great help.I appreciate it .I'm posting the edited code .
|
0

The count method was declared as variable and i've added with addition assignment operator.

Thank You for helping.

$ids = array(); $output = array();

    foreach ($response as $value) {
        if (!isset($ids[$value["id"]])) {
            $ids[$value["id"]] = count($output);
            $output[]          = $value;
        }
        else {
            $output[$ids[$value["id"]]]["count_of_invites"] += $value["count_of_invites"];
            $output[$ids[$value["id"]]]["user"]             = $value["user"];
        }
    }

Comments

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.