Change language

How do I create an array for JSON using PHP?

|
Array: There are three types of arrays in PHP, which are listed below:
  • Indexed Arrays
  • Associative Arrays
  • Multidimensional Arrays
For this purpose, we will use an associative array, which uses a key value type structure to store data. This key will be a string or an integer that will be used as an index to find the corresponding value in the array. 
The json_encode function is used to convert an array value to JSON. This feature was added in PHP5. In addition, you can do more nesting of the arrays as per your requirement. You can also create an array of arrays of objects with this function. 
As with JSON, everything is stored as a key-value pair. We will convert this PHP array key-value pair to JSON, which can be used to send a response from the REST API server.Below are examples for converting an array to JSON. 
Example-1:  
// Create an array that contains another
// array with a key-value pair $arr = array ( // Each array will be converted // to the object array ( "name" = > " Pa nkaj Singh " , " age " = > "20" ), array ( "name" = > "Arun Yadav" , "age" = > "21" ), array ( "name" = > "Apeksha Jaiswal" , "age" = > "20" ));  
// Function to convert array to JSON echo json_encode ( $arr );  
?>
Exit:
[{" name ":" Pankaj Singh "," age ":" 20 "}, {" name ":" Arun Yadav "," age ":" 21 "}, {" name ":" Apeksha Jaiswal "," age ": "20"}] 
Example 2:  
// Declare two-dimensional associativity
// array and initialization $arr = array ( " first " = > array ( "id" = > 1, " product_name " = > " Doorbell " , "cost" = > 199 ), "second" = > array ( "id" = > 2, "product_name" = > "Bottle" , "cost" = > 99 ), " third " = > array ( "id" = > 3, " product_name " = > " Washing Machine " , "cost" = > 7999 ));  
// Function to convert array to JSON echo json_encode ( $arr );  
?>
Exit:
{"first ": {" id ": 1," product_name ":" Doorbell "," cost ": 199}," second ": {" id ": 2," product_name ":" Bottle "," cost ": 99}, "third": {"id": 3, "product_name": "Washing Machine", "cost": 7999}} 

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method