-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighestSpending.php
More file actions
52 lines (40 loc) · 1021 Bytes
/
HighestSpending.php
File metadata and controls
52 lines (40 loc) · 1021 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
function getUserData($file = "users.json")
{
//if the file exists then convert the data into object;
if (file_exists($file)) {
//read content from the file
$jsonData = file_get_contents($file);
return json_decode($jsonData, true);
}
//if file doesnot exist return null;
else {
return null;
}
}
//get User information and store in variable userData
$userData = getUserData();
//find the maximum spending and along with the user name
function TopSender($userData){
$maxSpent=0;
$username = null;
foreach ($userData["users"] as $user) {
$totalSpent = 0;
foreach ($user["purchases"] as $purchase) {
$totalSpent += $purchase["price"];
}
//check the maximum spending
if($totalSpent>$maxSpent){
$maxSpent=$totalSpent;
$username=$user["name"];
}
}
return $username." "."Amount is"." ".$maxSpent;
}
if($userData){
echo "Top Sender:"." " .TopSender($userData);
}
else{
echo "No spending Found";
}
?>