G-Square Official
G-Square Official

Reputation: 25

Appending object into array in php

"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,

Upvotes: 2

Views: 113

Answers (2)

Ayush Jain
Ayush Jain

Reputation: 349

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"
    ]
  ]
]

Upvotes: 1

Danyal Sandeelo
Danyal Sandeelo

Reputation: 12391

$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.

Upvotes: 1

Related Questions