Reputation:
I have an array that looks like this :
[
{"value": {
"api_rev":"1.0",
"type":"router",
"hostname":"Router Hasselt",
"lat":50.9307,
"lon":5.33248,
"elev":50,
"aliases":[
{
"type":"wired",
"alias":"11.96.253.9"
}],
"community":"Antwerpen",
"attributes":{"firmware":""}}}]"
Is it possible to remove [{"value":
and obviously the closing of it at the end }]
and leave the rest as it is ? I tried unsetting "value"
but this actually removes everything which i understand why. But if there is a workaround i will appreciate !
I expect this :
{
"api_rev":"1.0",
"type":"router",
"hostname":"Router Hasselt",
"lat":50.9307,
"lon":5.33248,
"elev":50,
"aliases":[
{
"type":"wired",
"alias":"11.96.253.9"
}],
"community":"Antwerpen",
"attributes":{"firmware":""}}"
Upvotes: 0
Views: 120
Reputation: 4414
Try this,
<?php
$str='[
{"value": {
"api_rev":"1.0",
"type":"router",
"hostname":"Router Hasselt",
"lat":50.9307,
"lon":5.33248,
"elev":50,
"aliases":[
{
"type":"wired",
"alias":"11.96.253.9"
}],
"community":"Antwerpen",
"attributes":{"firmware":""}}}]';
$json=json_decode($str);
echo json_encode($json[0]->value);
?>
Test it here: DEMO
Upvotes: 0
Reputation: 550
This might help :)
$obj = json_decode($json);
echo json_encode($obj[0]->value);
Upvotes: 0
Reputation: 870
For example you have this:
$json = json_decode("your stuff");
Then you do:
$json = $json[0]->value;
Then you can encode it back:
$str = json_encode($json);
Upvotes: 2