-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTotalPrice.php
More file actions
48 lines (36 loc) · 894 Bytes
/
TotalPrice.php
File metadata and controls
48 lines (36 loc) · 894 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
<?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();
function getUserTotalSpent($userData,$userId){
$totalPrice=0;
foreach($userData["users"] as $user){
//check if the user id match then calculate price
if($user["id"]===$userId){
$totalPrice=0;
foreach($user["purchases"] as $purchase){
$totalPrice+=$purchase["price"];
}
}
}
return $totalPrice;
}
if($userData){
echo "Total Amount Spent By the User is:".getUserTotalSpent($userData,1);
}
else{
echo "No Amount Spent";
}
?>