I am developing a expense manager app, So the scenario is I want to show Summary screen for expense and Income. I have write code for this but problem is I want to store the category data like medicine, travel, communication in a particular variable specifically.
So when this For each loop executes next time next category value would be assigned in the item array. But my requirement is I want to store only specific category data in item variable. I explaining this what I want to say :- Each time when loop will execute, only those category data I want to show in Item variable For Ex- First category name is Medicine then the medicine information would be stored in item array. Next time if category is something else then it would be also store in item array that I don't want. And if medicine again comes then this data should be added to the previous data in the item array.
for each ($summary_expense as $s){
$c = $s['cat_name'];
if($c == $s['cat_name']){
$item[] = array(
'id' => $s['id'],
'user_id' => $s['user_id'],
'cat_name' => $s['cat_name'],
'item_name' => $s['item_name'],
'amount' => $s['amount'],
'payment_mode' => $s['p_mode'],
'account' => $s['account_name'],
'expense_notes'=> $s['expense_notes'],
'audio' => $s['audio'],
'image' => $s['image'],
'expense_date' => $s['expense_date'],
);
$total_expense = $s['amount'] + $total_expense;
$s['total'] = $total_expense;
$s['item'] = $item;
$expense[] = $s;
}
Response is :-
"expense": [
{
" "id": "2",
"user_id": "3",
"amount": "21",
"item_name": "Home Med\nHomeo",
"audio": "1590648985_AudioRecording.3gp",
"expense_notes": "",
"expense_date": "28-05-2020",
"image": "211500738374870.png",
"cat_name": "Personal expense",
"p_mode": "Cash",
"acc_name": "Khata 1",
"total": 2243,
"item": [
{
"id": "1",
"user_id": "3",
"cat_name": "Medicine",
"item_name": "fGzgy",
"amount": "2222",
"payment_mode": "Cheque",
"account": "Khata 2",
"expense_notes": "gzhzhzxh",
"audio": "1590565406.3gp",
"image": "1590565406.png",
"expense_date": "06-05-2020"
},
{
"id": "2",
"user_id": "3",
"cat_name": "Personal expense",
"item_name": "Home Med",
"amount": "21",
"payment_mode": "Cash",
"account": "Khata 1",
"expense_notes": "",
"audio": "abc.rar",
"image": "1590648985.png",
"expense_date": "28-05-2020"
}
]
Desired Output is:-
[
{
" "id": "2",
"user_id": "3",
"amount": "21",
"item_name": "Home Med\nHomeo",
"audio": "1590648985_AudioRecording.3gp",
"expense_notes": "",
"expense_date": "28-05-2020",
"image": "10738374870.png",
"cat_name": "Personal expense",
"p_mode": "Cash",
"acc_name": "Khata 1",
"total": 2243,
"item": [
{
"id": "1",
"user_id": "3",
"cat_name": "Personal expense",//Same category data
should be assign in item array
"item_name": "fGzgy",
"amount": "2222",
"payment_mode": "Cheque",
"account": "Khata 2",
"expense_notes": "gzhzhzxh",
"audio": "1590565406.3gp",
"image": "1590565406.png",
"expense_date": "06-05-2020"
},
{
"id": "2",
"user_id": "3",
"cat_name": "Personal expense",
"item_name": "Home Med",
"amount": "21",
"payment_mode": "Cash",
"account": "Khata 1",
"expense_notes": "",
"audio": "abc.rar",
"image": "1590648985.png",
"expense_date": "28-05-2020"
}
]