an0n1m0us.b4st4rd
an0n1m0us.b4st4rd

Reputation: 171

creating a json object in PHP

how do i create a json object similar to this in PHP?

{
    "employees": [
        { "firstName":"John" , "lastName":"Doe" }, 
        { "firstName":"Anna" , "lastName":"Smith" }, 
        { "firstName":"Peter" , "lastName":"Jones" }
    ]
}

Upvotes: 9

Views: 17373

Answers (1)

AD7six
AD7six

Reputation: 66170

You create an array, and pass it to json_encode

$stuff = array(
    'employees' => array(
        array('firstName' => 'John', 'lastName' => 'Doe'),
        ....
    )
);

echo json_encode($stuff);

Upvotes: 24

Related Questions