All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Convert Array To JSON PHP

Last Updated : Mar 11, 2024

Convert Array To JSON PHP

In this article we will show you the solution of convert array to JSON PHP, to achieve the result we need an array with multiple set of values for this topic, so we created associate type array.

In this array usually it holds various column values with respective values. To happen the conversion you need json_encode() method.

The json_encode() allows you to do the conversion of PHP data into JSON format. It takes php object and gives JSON string as output of result.

Step By Step Guide On Convert Array To JSON PHP :-

Create array variable as per your wish, to hold defining set of arrays and values. Three columns namely, (Age, Department, Work Experience) and two row arrays created.

You can change array values with set of information as like you want without fail and columns also not mandatory.

All you need to pay attention is define everything or modify in the correct format.

Finally using echo() method, you have to pass array variable to json_encode() method result will you get correctly.

<?php
$arr_data = array(
"Dhanu" => array("Age"=>25,"Department"=>"Writer","Work Experience (In Years)"=>2),
"Prawin" => array("Age"=>26,"Department"=>"Mechanic Engineer","Work Experience (In Years)"=>4),
"Arun" => array("Age"=>24,"Department"=>"Director","Work Experience (In Years)"=>1)
);
echo json_encode($arr_data);
?>
  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. In php we need to specify ‘$’ symbol before the variable name also default rule.
  3. First, we created variable $arr_data, here you can change this variable name as per your wish but keep in mind pre-defined rules for variable definition.
  4. This array holding three columns and two rows set of values ‘array( "Dhanu" => array("Age"=>25,"Department"=>"Writer","Work Experience (In Years)"=>2), "Prawin" => array("Age"=>26,"Department"=>"Mechanic Engineer","Work Experience (In Years)"=>4), "Arun" => array("Age"=>24,"Department"=>"Director","Work Experience (In Years)"=>1));’.
  5. In the line, the block of code can changeable but try to modify in a right manner to get error-free result.
  6. This associative array variable $arr_data needs to pass on json_encode() method. It enables conversion of array to JSON and returning JSON string you can check at result page.
  7. Instead of echo() you can prefer print() method if you want and that not going to affects your result.

Conclusion :-

In conclusion, hope now you know how to convert array to JSON using php.

When you work with php we need to create and process php files at server location and then we need to start the server before execute the program.

Otherwise sometimes you will get error so ensure it if you get any error while running output.

When we execute this program on browser it will show ‘{"Dhanu":{"Age":25,"Department":"Writer","Work Experience (In Years)":2}, "Prawin":{"Age":26,"Department":"Mechanic Engineer","Work Experience (In Years)":4}, "Arun":{"Age":24,"Department":"Director","Work Experience (In Years)":1}}’.

With this help of sample code you can test and get the output in the same type flawlessly.

I hope this article on convert array to JSON PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪