Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating a json object in PHP

Tags:

json

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" }
    ]
}
like image 219
an0n1m0us.b4st4rd Avatar asked May 15 '12 21:05

an0n1m0us.b4st4rd


People also ask

What is JSON object in PHP?

What is JSON? JSON stands for JavaScript Object Notation, and is a syntax for storing and exchanging data. Since the JSON format is a text-based format, it can easily be sent to and from a server, and used as a data format by any programming language.

Can PHP write to JSON file?

Using PHP scripting we can store a form value in array format. After that will convert the array into JSON data using json_encode() predefined function. Then at last we can move the data to JSON format file. Once all data get validated, we need to add in JSON file.

Why JSON is used in PHP?

JSON is portable because parsers and writers are available for many, many languages. This means that JSON that a PHP script generates can be very easily understood by a JavaScript script. It is the best way to transmit complex structures like arrays and objects, and have it still be compatible with multiple languages.

Does PHP support JSON?

The PHP FilePHP has some built-in functions to handle JSON.


1 Answers

You create an array, and pass it to json_encode

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

echo json_encode($stuff);
like image 53
AD7six Avatar answered Oct 11 '22 11:10

AD7six