All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Create JSON Array In PHP

Last Updated : Mar 11, 2024

How To Create JSON Array In PHP

In this article we will show you the solution of how to create JSON array in PHP, in PHP, an array is a kind of data structure that lets us store multiple elements with the same form under a single variable, saving us the time and effort of having to create a different variable by each data.

Using the index or key of an element in an array, it is possible to access a list of other elements with similar types.

In PHP,an array is created with the array() function. An array with a numeric key is known as an indexed array.

It functions essentially as an array where each key points to a different value.In an associative array, it stores key-value pairs.

An array of this type, known as a multidimensional array,stores another array at every index rather than just one element.

In other words, multidimensional arrays are defined as arrays of arrays. We will employ an associative array for this function, which stores data in a key-value type structure.

These keys, which can be either strings or numbers, will be used as indexes to find the corresponding values in the array.

The array's value is converted into JSON using the json encode() function.

Users will convert these PHP arrays' key-value pairs to JSON, where each value is stored as just a key-value pair and can be used to send the REST API server's response.

Step By Step Guide On How To Create JSON Array In PHP :-

<?php
 $arr = array (
     array(
          "name" => "Anuj Shahane",
          "age" => "20"
      ),
      array(
          "name" => "Ron Weasley",
          "age" => "21"
      ),
      array(
          "name" => "Harry potter",
          "age" => "20"
      )
  );
  echo json_encode($arr);
?>
  1. Tags with short identifiers begin with "<?" and end with "?>". php.ini configuration files on servers should have short style tags enabled.
  2. Then we Create an array that contains another array with key value pair
  3. After that we Define an object with the property and value "limit".
  4. Then we see Every array will be converted to an object
  5. After that we create a JSON string from the object.
  6. Then the JSON string is a parameter in a request that is sent to the PHP file.
  7. Wait for the request to return this same result (as JSON) and we see Function to convert array into JSON
  8. After creation Display the output from the PHP file.

Conclusion :-

JSON is frequently used to display data in web pages by reading it from a web server. PHP is a database-connected server-side programming language.

Consider sending a request from the client to a database on one's server asking for the first 10 rows of a table called "customers."

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

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪