PHP json_encode() Function

PHP json_encode() Function is used to encode array value to JSON format. A json_encode() function is built-in function in PHP.

Syntax :

json_encode(array $array);

Parameter,

$array: Required. It is a input array.

Return Values :

It returns a JSON object or json array for associative array and numeric array respectively.

 Objects in PHP can be converted into JSON by using the PHP function json_encode().

Let's see below example to understand php json_encode() Function in details.

Example :

<?php
echo "<br>Below example shows how to encode an associative array into a JSON object <br>";
$marks= array("John"=>89, "Herry"=>56, "roze"=>77, "Jack"=>55);
echo json_encode($marks);

echo "<br><br>Below example shows how to encode an numeric array into a JSON array<br>";
$tutorials= array("HTML", "CSS", "JAVA", "PHP","MYSQL");
echo json_encode($tutorials);
?>

Output :

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

11632