2

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.

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

Comments

0

Try with -

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.