I'm trying to add a new key-value-pair at the end of every users element:
<?php
$json = '[
{
"date": "2014-10-09T17:38:19Z",
"users": [
{
"name": "Peter",
"age": 20
},
{
"name": "Anne",
"age": 25
},
{
"name": "William",
"age": 30
}
]
}
]';
addData ( $json );
function addData($json) {
$obj = json_decode ( $json, true );
foreach ( $obj as $items ) {
foreach ( $items ['users'] as $users ) {
$array = array (
"myKey" => "myValue"
);
array_push ( $users, $array );
}
}
$json = json_encode ( $obj );
echo $json;
}
?>
So the new json should look like
[
{
"date":"2014-10-09T17:38:19Z",
"users":[
{
"name":"Peter",
"age":20,
"myKey":"myValue"
},
{
"name":"Anne",
"age":25,
"myKey":"myValue"
},
{
"name":"William",
"age":30,
"myKey":"myValue"
}
]
}
]
Instead, I get my old json as an output, without the new key-value-pair.