hi,
i'm trying to use json_decode to combine a few json objects and then re-encode it. my json looks like:
{
"core": {
"segment": [
{
"id": 7,
"name": "test1"
},
{
"id": 4,
"name": "test2"
}
]
}
}
i have a few of these json objects and would like to combine only the "segement" arrays for each to get something like this:
{
"segment": [
{
"id": 7,
"name": "test1"
},
{
"id": 4,
"name": "test2"
}
],
"segment": [
{
"id": 5,
"name": "test3"
},
{
"id": 8,
"name": "test4"
}
]
}
right now in my php code, i'm decoding the json, storing each "segment" array into a string, and then encoding the json.
public function handleJSON($json){
$decodeData = json_decode($json);
$segment =$decodeData->core;
return $segment;
}
public function formatJSON(){
$segments = "";
for ($i = 0; $i < count($json);$i++)
{
$segments .= handleJSON($json[$i]);
}
echo json_encode($segments);
}
when i do this, i receive an error : Object of class stdClass could not be converted to string
so then i tried using storing them in an array:
public function formatJSON(){
$segments = array();
for ($i = 0; $i < count($json);$i++)
{
$segments[$i] = handleJSON($json[$i]);
}
echo json_encode($segments);
}
this time, i don't get an error, but it stores my entire combined json object in array brackets. how can i have it just return the JSON object, without being encapsulated in an array?