<?php $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5); print_r($arr); print("<br>"); echo json_encode($arr); ?>
Array ( [a] => 1 [b] => 2 [c] => 3 [d] => 4 [e] => 5 )
{"a":1,"b":2,"c":3,"d":4,"e":5}
<?php
$response=array(); $response["success"] = 0; $response["message"] = "No product found"; //print_r($product); print("<br><font color='red'> \$response in array Format </font> <br>"); //print_r — Prints human-readable information about a variable print_r($response); print("<br><br><font color='red'> JSON Format </font> <br>"); //json_encode — Returns the JSON representation of a value //cpnvert array to json format print json_encode($response); ?>
$response in array Format
Array ( [success] => 0 [message] => No product found )
JSON Format
{"success":0,"message":"No product found"}
Use $response
$response=array();
array_push($response,$product);
<?php $response=array(); $product = array(); $product["pid"] = 123; $product["name"] = "ABC"; $product["price"] = 33.0; $product2 = array(); $product2["pid"] = 123; $product2["name"] = "ABC"; $product2["price"] = 33.0; //print_r($product); /* Read php manual http://php.net/manual/en/function.array-push.php array_push() treats array as a stack, and pushes the passed variables onto the end of array. The length of array increases by the number of variables pushed. */ // array of $product was pushed or added on to the end of $reponse array array_push($response,$product); array_push($response,$product2); print("<br><font color='red'> \$response in array Format </font> <br>"); //print_r — Prints human-readable information about a variable print_r($response); print("<br><br><font color='red'> \$response in array Format </font> <br>"); //add one more field [success] to the $response array $response["success"] =1; print_r($response); print("<br><br><font color='red'> JSON Format </font> <br>"); //json_encode — Returns the JSON representation of a value //cpnvert array to json format
print json_encode($response); ?>
Output Results
$response in array Format
Array ( [0] => Array ( [pid] => 123 [name] => ABC [price] => 33 ) [1] => Array ( [pid] => 123 [name] => ABC [price] => 33 ) )
$response in array Format
Array ( [0] => Array ( [pid] => 123 [name] => ABC [price] => 33 ) [1] => Array ( [pid] => 123 [name] => ABC [price] => 33 ) [success] => 1 )
JSON Format
{"0":{"pid":123,"name":"ABC","price":33},"1":{"pid":123,"name":"ABC","price":33},"success":1}
https://www.tutorialspoint.com/php_webview_online.php
Use $response["products"]
$response["products"] = array();
array_push($response["products"], $product);
<?php $response["products"] = array(); $product = array(); $product["pid"] = 123; $product["name"] = "ABC"; $product["price"] = 33.0; $product2 = array(); $product2["pid"] = 124; $product2["name"] = "DEF"; $product2["price"] = 88.0; array_push($response["products"], $product); array_push($response["products"], $product2); $response["success"] = 1; print("<br><font color='red'> \$response in array Format </font> <br>"); //print_r — Prints human-readable information about a variable print_r($response); print("<br><br><font color='red'> JSON Format </font> <br>"); //json_encode — Returns the JSON representation of a value //cpnvert array to json format print json_encode($response); ?>
$response in array Format
Array ( [products] => Array ( [0] => Array ( [pid] => 123 [name] => ABC [price] => 33 ) [1] => Array ( [pid] => 124 [name] => DEF [price] => 88 ) ) [success] => 1 )
JSON Format
{"products":[{"pid":123,"name":"ABC","price":33},{"pid":124,"name":"DEF","price":88}],"success":1}