Sushant Aryal
Sushant Aryal

Reputation: 3335

Removing a outer array from JSON object - PHP

I have fetched Google Analytics profiles from my account, and returned an JSON object to my view. But I want to convert this object.

[
    {
        Account A: {
            xxxxxxxx: "All Web Site Data"
        }
    },
    {
        Account B: {
            xxxxxxxx: "All Web Site Data"
        }
    }
]

to this

{
    Account A: {
        xxxxxxxx: "All Web Site Data"
    }
},
{
    Account B: {
        xxxxxxxx: "All Web Site Data"
    }
}

How can i do this using PHP.

Upvotes: 2

Views: 1685

Answers (2)

Girish
Girish

Reputation: 12127

Your json object looking incorrect, try this way

<?php 

$jsond = '[{ "AccountA": {"xxxxxxxx": "All Web Site Data"}},{"AccountB": {"xxxxxxxx": "All Web Site Data"}}]';
$arr = json_decode(trim($jsond));
//echo json_last_error_msg ();
print_r($arr[0]);
?>

Simply use [0] index object

JavaScript

var json = json[0];
console.log(json);

PHP

var $json = $json[0];
var_dump($json);

Upvotes: 2

Sougata Bose
Sougata Bose

Reputation: 31749

Try with -

$json = (array) json_decode($yourJson);
var_dump($json);

Upvotes: 0

Related Questions