I am facing an issue. In the JSON array given below:
$results = [
{
proj_name: rental,
act_name: income,
amount: "1000"
},
{
proj_name: loan,
act_name: income,
amount: "7000"
},
{
proj_name: rental,
act_name: initiall,
amount: "3000"
},
{
proj_name: loan,
act_name: expend,
amount: "-11000"
},
{
proj_name: rental,
act_name: income,
amount: "4000"
},
{
proj_name: rental,
act_name: expend,
amount: "-5000"
},
{
proj_name: rental,
act_name: initial,
amount:"6000"
},
{
proj_name: loan,
act_name: expend,
amount: "-8000"
},
{
proj_name: loan,
act_name: initial,
amount: "9000"
},
{
proj_name: rental,
act_name: expend,
amount: "-2000"
},
{
proj_name: loan,
act_name: income,
amount: "10000"
},
{
proj_name: loan,
act_name: initial,
amount:"12000"
}
];
We can see here we have two proj_name
as rental
and loan
.
Also transactions are categorized further with in activities as income
, expend
and initial
.
My question is how I can loop through $results
array to calculate sum for each transaction type that I listed above. I would like the output to be like this:
|------------------|
| rental |
|------------------|
|income : 5000 |
|expend : -7000 |
|initial : 9000 |
|------------------|
|Sum Rental: 7000 |
|__________________|
|------------------|
| loan |
|------------------|
|income : 17000 |
|expend : -19000 |
|initial : 21000 |
|------------------|
|Sum Loan: 19000 |
|__________________|
And I want to create new array as in this form:
$results = [
-{
projname: rental,
income: 5000,
expend : -7000,
initial : 9000,
sum : 7000
},
-{
projname: loan,
income: 17000,
expend : -19000,
initial : 21000,
sum : 19000
}
];