在PHP中创建一个json对象

时间:2012-05-15 21:54:42

标签: php json

如何在PHP中创建与此类似的json对象?

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

1 个答案:

答案 0 :(得分:21)

您创建一个数组,并将其传递给json_encode

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

echo json_encode($stuff);