All TalkersCode Topics

Follow TalkersCode On Social Media

PHP Create json Object

Last Updated : Jan 1, 2023

PHP Create json Object

In this tutorial we will show you the solution of PHP create json object, as we know JSON object is a syntax for storing and exchanging data. Since the JSON format is text-based format, it can easily be sent to and from a server and used as a data format by any programming language.

Here we defined associated array with some values and using json_encode() method we encoding json in php. This function returns json representation of a value.

Step By Step Guide On PHP Create json Object :-

Here we defined associative array namely ‘$arr’ with values ‘"John"=>34,"Prawin"=>25,"Jazz"=>19’.

Then using json_encode() method we converting associative array values into json format values.

This method had two parameters first one is value being encoded second one is optional value is a bitmask consisting of more number of options like JSON_HEX_TAG, JSON_HEX_AMP,..etc. We can also decode the json string.

list($inp1,$inp2)=explode(' ',readline());
$name=(string)readline('Enter Your Name: ');
echo "Hello ".$name." Your integer input is: ".$inp1. " and floating point is ".$inp2;
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. In php when define a variable we need to use ‘$’ symbol before variable name and we don’t need to add data types because it can automatically associates data type to the variable depending on its value.
  4. Here we defined associative array and named this array as ‘$arr’ then we initialized some keys and respective values ("John"=>34,"Prawin"=>25,"Jazz"=>19).
  5. Then we using json_encode() method and passed parameter as associative array ‘$arr’. So it is converted into json format and displayed converted result using echo() method.
  6. As we know json object is a container for other variables it contains a list of key, value pairs separated by colon. The keys are always string and enclosed in double quotes.
  7. So we can see our converted result has been same as aspect syntax format and we can verify them easily.
  8. We can also define array with combination of more number of key and respective values. You can define array with any key, values it all depends on our own wish we can modify it.

Conclusion :-

In conclusion we are able to know how to create json object in php. First we need to start our xampp server then we need to load this program on browser we can see result of converted json format array values.

We can also define json object with contains one or more json object and use this same concept we can get result it won’t affect the result later we will see in detail about this.

We can define any types of json array using php try to explore different contents of json array like books, authors or random contents,.etc.

I hope this tutorial on PHP create json object helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials