1

I want a json output as follows:

{
    "id": "c200",
    "name": "Aneesh",
    "email": "[email protected]",
    "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            },
    "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
}

how to write a PHP code using json_encode for this output? Thanks! Cheers!

2
  • 2
    you can't have 2 phone keys, alternatively, you can add another dimension inside phone, then you can have them both Commented Aug 16, 2014 at 7:05
  • Its just an example, my actual data is something different and a bit complex. If you'll could answer this, it might just be helpful with the actual data. Commented Aug 16, 2014 at 7:08

4 Answers 4

2

As I have said in the comments, that won't be possible because they share the same key.

Alternatively, you could create another dimension inside phone. Example:

$values = array(
    'id' => 'c200',
    'name' => 'Aneesh',
    'email' => '[email protected]',
    'phone' => array(
        array(
            "mobile" => "+91 0000000000",
            "home" => "00 000000",
            "office" => "00 000000",
        ),
        array(
            "mobile" => "+91 0000000000",
            "home" => "00 000000",
            "office" => "00 000000",
        ),
    ),
);

echo '<pre>';
echo json_encode($values, JSON_PRETTY_PRINT);

Should output this:

{
    "id": "c200",
    "name": "Aneesh",
    "email": "[email protected]",
    "phone": [
        {
            "mobile": "+91 0000000000",
            "home": "00 000000",
            "office": "00 000000"
        },
        {
            "mobile": "+91 0000000000",
            "home": "00 000000",
            "office": "00 000000"
        }
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

0

Jut create an array and pass it to the json_decode, like this:

$arr = array("id"=> "c200", "name"=> "Aneesh", "email"=> "[email protected]",
"phone"=>array("mobile"=> "+91 0000000000", "home" =>"00 000000", "office"
=>"00 000000"));

echo json_encode($arr);

Comments

0

The easiest way to get JSON output with PHP, is to simply make a PHP array, and then echo json_encode($array);.

To explain, for your code:

$array = array(  "id"=>"c200",
                 "name"=>"Aneesh",
                 "email"=> "[email protected]",
                 "phone"=> array(array(  "mobile"=>"+91 0000000000",
                                         "home"=> "00 000000",
                                         "office"=>"00 000000"),
                                 array(  "mobile"=>"+91 0000000000",
                                         "home"=> "00 000000",
                                         "office"=>"00 000000"
)));

echo json_encode($array);

The nice thing about json_encode is it will take care of special characters that need to be escaped in JSON for you, so you can put whatever you want in the array.

Comments

-1

You can get an idea from this:

var arr ={};
arr['name'] = "random";
arr['contact']= {};
arr['contact']['first']="3333333";
arr['contact']['second']="2222222";

console.log(arr);

3 Comments

console.log is used for debugging , for your information
Please do not use 'var' keyword, it has been depreciated in PHP 5.x
@rahulmr he using PHP not JS/jQuery

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.