0

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"
                        }
                    ]
Saif Ali
  • 1
  • 2
  • Can you show desired output? – Jsowa Jun 09 '20 at 08:28
  • just think there are multiple items for id:2,id:3 and the format of this are same as above like id:1. If the same category found like medicine then medicine category data should be assign in item array if other category found then that should be stored in item array but medicine category data should not be come in item array.. This is little bit messy I think. – Saif Ali Jun 09 '20 at 10:10
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Jun 09 '20 at 10:34
  • It is messy, so please provide current output (actually you did this) vs. desired output. – Jsowa Jun 09 '20 at 10:51
  • please look this. I have provided the desired output – Saif Ali Jun 10 '20 at 05:14

0 Answers0