PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 17, 2022

[FIXED] Calculate the total actions from a user in a array using PHP

 January 17, 2022     arrays, associative-array, calculation, php     No comments   

Issue

I have an array that is a collection of user actions. I would like to slice out each user and the total amount of their actions..

The array has 3 values

  • Sender
  • Value
  • function

Then there are 3 functions possible -

  • Deposit
  • Withdraw
  • Remove

A user can deposit money, take some money out (Withdraw) or remove all (Remove)

Here is the associative array -

$resultArray[] = array(
    'sender' => $sender,
    'value' => $value,
    'function' => $func,
);

What I am trying to do is for each user in the array calculate how much money was inputed, withdrawn or removed in sequential order. So if user A inputs 10 dollars, then withdraws 5, then inputs 2 the total for User A would be 7 dollars.

I am struggling to find the correct logic and method to accomplish this. Anyone have some insight on how I can achieve the desired results for each user?

Here is a sample array

Array ( [0] => Array ( [sender] => userA [value] => 10 [function] => deposit ) [1] => Array ( [sender] => userB [value] => 5 [function] => deposit ) [2] => Array ( [sender] => userA [value] => 2 [function] => withdraw )[3] => Array ( [sender] => userA [value] => 3 [function] => deposit )   

The desired result from the sample array would be -

  • User A = 11
  • User B = 5

Thank you!


Solution

You could for example make use of array_reduce (or a foreach loop using the same approach) and for the $result array take the sender as the array key.

Assuming only positive numbers, if there is no sender yet and there is a deposit, set that value as the start value. If there is a withdraw, then negate the value.

If there already is a sender, then according to the value of deposit or withdraw add or subtract the value.

$result = array_reduce($arrays, function ($acc, $curr) {
    if (array_key_exists($curr["sender"], $acc)) {
        if ($curr["function"] === "deposit") {
            $acc[$curr["sender"]] += $curr["value"];    
        }
        if ($curr["function"] === "withdraw") {
            $acc[$curr["sender"]] -= $curr["value"];
        }
    } else {
        if ($curr["function"] === "deposit") {
            $acc[$curr["sender"]] = $curr["value"];
        }
        if ($curr["function"] === "withdraw") {
            $acc[$curr["sender"]] = -1 * $curr["value"];
        }
    }
    return $acc;
}, []);

Output

Array
(
    [userA] => 11
    [userB] => 5
)

See a PHP demo.



Answered By - The fourth bird
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing