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?
The json_decode() function is used to decode or convert a JSON object to a PHP object.
The json_decode() function can return a value encoded in JSON in appropriate PHP type. The values true, false, and null is returned as TRUE, FALSE, and NULL respectively. The NULL is returned if JSON can't be decoded or if the encoded data is deeper than the recursion limit.
If json_decode returns null is it because the database. json is not valid. You can fix this by open the database.
To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.
I think one approach would be to take advantage of the second parameter to json_decode, assoc
:
"When TRUE, returned objects will be converted into associative arrays."
I find that usually it's easier to deal with associative arrays rather than a stdClass
class.
$str = '{
"core": {
"segment": [
{
"id": 7,
"name": "test1"
},
{
"id": 4,
"name": "test2"
}
]
}
}';
print "<pre>";
print_r(json_decode($str));
print "</pre>";
print "<pre>";
print_r(json_decode($str,true));
print "</pre>";
This produces first the Object version, then the associative array:
stdClass Object
(
[core] => stdClass Object
(
[segment] => Array
(
[0] => stdClass Object
(
[id] => 7
[name] => test1
)
[1] => stdClass Object
(
[id] => 4
[name] => test2
)
)
)
)
Array
(
[core] => Array
(
[segment] => Array
(
[0] => Array
(
[id] => 7
[name] => test1
)
[1] => Array
(
[id] => 4
[name] => test2
)
)
)
)
I think I would do something like, create new blank array, decode as associative array, grab out the segment members and splice them into the new blank array. So:
$segments = array();
// assuming you had a bunch of items in the $strings array
foreach ($strings as $str) {
$item = json_decode($str,true);
$segments = array_merge($item['core']['segment], $segments);
}
Now, you can encode this to json like this:
$final_json = json_encode(array('segments'=>$segments));
Your outer object contains two items named "segment". While this is legal JSON it is not possible to have a PHP object with two different items with the same name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With