Anirudh Anirudh - 1 month ago 8
PHP Question

How to add a new key value pair to every object inside an array in php?

My code:
Scenario 1

$newarray = array();

foreach($rows as $k => $v)
{
$newarray[$k] = $v;
$newarray['newitem'] = 'Add this to existing object';
}
echo json_encode($newarray);


I tried this as well:
Scenario 2

$newarray = array();

foreach($rows as $k => $v)
{
$newarray[$k] = $v;
$newarray[$k]['newitem'] = 'Add this to existing object';
}
echo json_encode($newarray);


In scenario 1, key value pair gets added for after 1st object only. It doesn't loop through.
In scenario 2, i get HTTP ERROR 500

What am I doing wrong here ?

Update:

Existing Output:

[{"pid":"123","date":"2016-08-23 08:08:40","post_title":"AHHH"},{"pid":"223","date":"2016-08-23 08:08:40","post_title":"wwwwAHHH"},{"pid":"223","date":"2016-08-23 08:08:40","post_title":"xxxAHHH"}]


Required Output:

[{"pid":"123","date":"2016-08-23 08:08:40","post_title":"AHHH","newkey":"new value1"},{"pid":"223","date":"2016-08-23 08:08:40","post_title":"wwwwAHHH","newkey":"new value2"},{"pid":"223","date":"2016-08-23 08:08:40","post_title":"xxxAHHH","newkey":"new value3"}]

Answer

You can make an temporary array containing what you want and then add it to the newarray, so create a temporary array, add the new data and the existing data, then add that to your newarray

$newarray = array();
foreach($rows as $k => $v) 
{
    $t = $v;
    $t['newitem'] = 'Add this to existing object';
    $newarray[$k] = $t;
}
echo json_encode($newarray);

EDIT:

Now I know what is in $rows is a JSON String this is the way to do what you want. You cannot foreach over a JSON string, you must first convert it to a PHP data structure, then foreach over it adding the new data, then convert it back to a JSON String

$rows = '[{"pid":"123","date":"2016-08-23 08:08:40","post_title":"AHHH"},{"pid":"223","date":"2016-08-‌​23 08:08:40","post_title":"wwwwAHHH"},{"pid":"223","date":"2016‌​-08-23 08:08:40","post_title":"xxxAHHH"}]';

$objArray = json_decode($rows);

$newarray = array();
foreach($objArray as $v) 
{

    $v->newitem = 'Add this to existing object';
    $newarray[] = $v;
}
print_r($newarray);
echo json_encode($newarray);

The $newArray is now

Array
(
    [0] => stdClass Object
        (
            [pid] => 123
            [date] => 2016-08-23 08:08:40
            [post_title] => AHHH
            [newitem] => Add this to existing object
        )

    [1] => stdClass Object
        (
            [pid] => 223
            [date] => 2016-08-‌​23 08:08:40
            [post_title] => wwwwAHHH
            [newitem] => Add this to existing object
        )

    [2] => stdClass Object
        (
            [pid] => 223
            [date] => 2016‌​-08-23 08:08:40
            [post_title] => xxxAHHH
            [newitem] => Add this to existing object
        )

)

And the new JSON String will be

[  {"pid":"123",
    "date":"2016-08-23 08:08:40",
    "post_title":"AHHH",
    "newitem":"Add this to existing object"
   },
   {"pid":"223",
    "date":"2016-08-\u200c\u200b23 08:08:40",
    "post_title":"wwwwAHHH",
    "newitem":"Add this to existing object"
   },
   {"pid":"223",
    "date":"2016\u200c\u200b-08-23 08:08:40",
    "post_title":"xxxAHHH",
    "newitem":"Add this to existing object"
   }
]