2
"data": {
    "advance_amount": [],
    "collection_report": [
        {
            "value": 7,
            "date": "2018-07-10",
            "paid_amount": "3510",
            "totalAmount": 4550,
            "pending_amount": 990
        },
        {
            "value": 8,
            "date": "2018-08-01",
            "paid_amount": "1998",
            "totalAmount": 7255,
            "pending_amount": 3986
        },
        {
            "value": 9,
            "date": "2018-09-14",
            "paid_amount": "1157",
            "totalAmount": 2272,
            "pending_amount": 1046
        },
        {
            "advance_amount": "25"
        },
        {
            "advance_amount": null
        },
        {
            "advance_amount": "5225"
        }

This is my response. But I want to append those advance amount in each collection_report after pending amount.

In this way

"value": 7,
"date": "2018-07-10",
"paid_amount": "3510",
"totalAmount": 4550,
"pending_amount": 990,
"advance_amount": 123,
4
  • 1
    Please add the code which generates the data, it's impossible to fix this without it (except to reprocess the output). Commented Dec 24, 2018 at 11:05
  • Where have you get 123 ? Commented Dec 24, 2018 at 11:15
  • 123 is dummy value :) Commented Dec 25, 2018 at 3:23
  • @NigelRen : This is how my code looks $data["collection_report"] = "some result"; foreach ($data["collection_report"] as $key => $value) { $advance = \DB::table('table name') ->select(\DB::raw("sum(column name)AS name")) ->first(); <!-- Here I want to merge result of advance inside collection report --> } Commented Dec 25, 2018 at 3:38

2 Answers 2

1
$array= json_decode($json);
$array['collection_report'][0]['pending_amount']=25;
$array['collection_report'][1]['pending_amount']=null;
$array['collection_report'][2]['pending_amount']=5225;

This is just for your understanding. Covert back the json to Array, iterate over the items (each item is an array), jut add a new key=>value in the array

use array_key_exists to check if keys exists or not.

Sign up to request clarification or add additional context in comments.

Comments

1

You can easily achieve this by first using json_decode() function to get the output in Array format and then a simple for loop would do your task as:

$initialInput =  '
        {
            "advance_amount": [],
            "collection_report": [
                {
                    "value": 7,
                    "date": "2018-07-10",
                    "paid_amount": "3510",
                    "totalAmount": 4550,
                    "pending_amount": 990
                },
                {
                    "value": 8,
                    "date": "2018-08-01",
                    "paid_amount": "1998",
                    "totalAmount": 7255,
                    "pending_amount": 3986
                },
                {
                    "value": 9,
                    "date": "2018-09-14",
                    "paid_amount": "1157",
                    "totalAmount": 2272,
                    "pending_amount": 1046
                },
                {
                    "advance_amount": "25"
                },
                {
                    "advance_amount": null
                },
                {
                    "advance_amount": "5225"
                }
             ]
        }        
       ';

Code:

$initialInput = json_decode($initialInput, true);
           for($i = 0; $i < count($initialInput['collection_report'])/2;$i++) {
               $initialInput['collection_report'][$i]['advance_amount'] = $initialInput['collection_report'][count($initialInput['collection_report'])/2 + $i]['advance_amount'];
           }

Final Output:

array:2 [▼
  "advance_amount" => []
  "collection_report" => array:6 [▼
    0 => array:6 [▼
      "value" => 7
      "date" => "2018-07-10"
      "paid_amount" => "3510"
      "totalAmount" => 4550
      "pending_amount" => 990
      "advance_amount" => "25"
    ]
    1 => array:6 [▼
      "value" => 8
      "date" => "2018-08-01"
      "paid_amount" => "1998"
      "totalAmount" => 7255
      "pending_amount" => 3986
      "advance_amount" => null
    ]
    2 => array:6 [▼
      "value" => 9
      "date" => "2018-09-14"
      "paid_amount" => "1157"
      "totalAmount" => 2272
      "pending_amount" => 1046
      "advance_amount" => "5225"
    ]
    3 => array:1 [▼
      "advance_amount" => "25"
    ]
    4 => array:1 [▼
      "advance_amount" => null
    ]
    5 => array:1 [▼
      "advance_amount" => "5225"
    ]
  ]
]

3 Comments

This answer is more generalized and hence a bit more useful if no of records in your JSON is variable.
what is the $intialInput here
Hi @G-SquareOfficial , its the input data you have provided (the JSON), you can have any name you like

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.