0

I currently have the following array:

Array
(
[0] => Array
    (
        [declaration_value] => 1
        [date] => 2018-07-16
        [client_id] => 3
        [declaration_id] => 12
    )

[1] => Array
    (
        [declaration_value] => 3
        [date] => 2018-07-16
        [client_id] => 3
        [declaration_id] => 12
    )

)

how can i make to get the following array result: (count declaration_value if the same date/client_id/declaration_id )

Array
(
[0] => Array
    (
        [declaration_value] => 4
        [date] => 2018-07-16
        [client_id] => 3
        [declaration_id] => 12
    )
)
5
  • 1
    have a look up this - stackoverflow.com/questions/51320014 Commented Jul 17, 2018 at 14:38
  • 2
    The idea here at SO is that you write the code! If it does not work as you hoped, you ask for help and we try and help you fix it. But but we dont write code for you Commented Jul 17, 2018 at 14:39
  • Basically in almost all cases if your question does not have some code in it attempting to code a solution, it is off topic Commented Jul 17, 2018 at 14:40
  • Can you show the code that creates that array? Ideally you would correct that code so that it creates the array you want instead of creating something you don't want and then fixing it. Commented Jul 17, 2018 at 15:29
  • @marmeladze that is very usefull, thanks Commented Jul 17, 2018 at 16:53

1 Answer 1

1
$listdb = [
        ["declaration_value" => 1, "date" => "2018-07-16", "client_id" => 3, "declaration_id" => 12],
        ["declaration_value" => 2, "date" => "2018-07-16", "client_id" => 2, "declaration_id" => 12],
        ["declaration_value" => 2, "date" => "2018-07-16", "client_id" => 2, "declaration_id" => 12],
        ["declaration_value" => 8, "date" => "2018-07-17", "client_id" => 2, "declaration_id" => 12],
        ["declaration_value" => 3, "date" => "2018-07-16", "client_id" => 3, "declaration_id" => 12],
    ];

    $sameKeys = ["date", "client_id", "declaration_id"];
    $sumKeys = ["declaration_value"];
    print_r(sum_my($listdb, $sameKeys, $sumKeys));

function sum_my(array $listdb = [], array $sameKeys = [], array $sumKeys = []): array {
    $newdb = [];
    if (empty($listdb) === true || empty($sameKeys) === true || empty($sumKeys) === true) {
        return $newdb;
    }

    foreach ($listdb as $value) {
        $ckKey = "";
        foreach ($sameKeys as $sameKey) {
            $ckKey .= $value[$sameKey];
        }
        if (isset($newdb[$ckKey])) {
            foreach ($sumKeys as $sumKey) {
                $newdb[$ckKey][$sumKey] += $value[$sumKey];
            }
        } else {
            $newdb[$ckKey] = $value;
        }
    }

    return $newdb;
}

Thank you for your tips, I solved it.

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

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.