Thursday 27 July 2023

PHP Stock Contribution Logic | How To Split The Quantity In Sequence


 
PHP Contribution Logic | How To Split The Contribution In Sequence
-------------------------------

Code :

<?php
$friends = array("TONY STARK"=>500,"STEVE ROGERS"=>210,"NATASHA"=>290);
$foods = array("SHWARMA"=>450,"PIZZA"=>300,"BURGER"=>250);
echo "<pre>";print_r($friends);echo "</pre>";
echo "<pre>";print_r($foods);echo "</pre>";

while(sizeof($friends)>0)
{
$first_friend = array_key_first($friends);
$first_friend_amount = $friends[$first_friend];

//echo $first_friend." - ".$first_friend_amount;


$first_food = array_key_first($foods);
$first_food_amount = $foods[$first_food];

//echo $first_food." - ".$first_food_amount;
$final_amount = 0;
if($first_friend_amount<$first_food_amount)
{
$final_amount = $first_friend_amount;
}
else
{
$final_amount = $first_food_amount;
}

echo "<div>".$first_friend." - ".$first_food." - ".$final_amount."</div>";

$first_friend_amount = $first_friend_amount - $final_amount;
$friends[$first_friend] = $first_friend_amount;

$first_food_amount = $first_food_amount - $final_amount;
$foods[$first_food] = $first_food_amount;

if($friends[$first_friend]<=0)
{
unset($friends[$first_friend]);
}
if($foods[$first_food]<=0)
{
unset($foods[$first_food]);
}

//echo "<pre>";print_r($friends);echo "</pre>";
//echo "<pre>";print_r($foods);echo "</pre>";

//exit;
}
?>

-------------------------------

Copy The Code:-
Subscribe my Channel:-
http://www.youtube.com/user/SanketRooney?sub_confirmation=1
Facebook Page:-
https://www.facebook.com/UnpossibleNS
Twitter Account:-
https://twitter.com/UnpossiblePOG
Blog:-
https://unpossiblepog.blogspot.com/
Website:-
https://unpossiblepog.com/
Gaming Instagram:- (@unpog.gaming)
https://www.instagram.com/unpog.gaming/

========================
Hey guys, This is Unpossible POG.
Today I am going to demonstrate the contribution split logic using PHP language.
So suppose there are 2 arrays.
1st array represents people who contributes money for the food.
Their contribution’s total is 1000.
The 2nd array is the food’s amount in sequence.
So, the output we want is something like this.
Because TONY STARK spent 500, and Shawarma’s cost is 480, he actually paid full money for shwarama. And because of it 20 amount is left.
So it is adjusted to next food item which is PIZZA. Now PIZZA’s amount is reduced from 300 to 280.
Next is STEVE ROGERS, who spent all his money on PIZZA. And so on.
Here is the code.
We will be using a WHILE loop which is risky one if we don’t break it. So just add exit at the end which will be temporary.
Any ways, the logic is to find first elements from each array, in this case, a first person and first food.
I will be using some short-cuts to avoid typing.
Let’s print first elements.
We will create a variable which will hold the least amount of person or food. So let’s compare first amounts from both array.
Assign which one is the least to FINAL AMOUNT.
Let’s print.
Now, we have to deduct that FINAL AMOUNT from both array’s first amount.
And overwrite the first element’s amount from both arrays.
Let’s run to see the current situation.
SHWARMA’s amount is set to zero and TONY STARK’s amount is reduced down to 50.
If any PERSON AMOUNT or FOOD AMOUNT becomes zero after the subtraction, we will unset that element using UNSET function.
Now, arrays are overwritten.
Now we can remove exit from the bottom and see the result.
That’s it.
Code link is given in the description.
Thanks for watching. Like, Share and Subscribe.

No comments:

Post a Comment