Q:

PHP program to demonstrate the json_encode() function with indexed array of strings

belongs to collection: PHP JSON Programs

0

Here, we will create an indexed array that contains the name of countries and then convert the array into JSON format using json_encode() function.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to demonstrate the json_encode() function with an indexed array of strings is given below. The given program is compiled and executed successfully.

<?php
//PHP program to demonstrate the json_encode() 
//function with indexed array of strings.
$Countries = array(
    "INDIA",
    "AUSTRALIA",
    "USA",
    "RUSSIA"
);
$jsonStr = json_encode($Countries);
printf("Result: %s<br>", $jsonStr);
?>

Output:

Result: ["INDIA","AUSTRALIA","USA","RUSSIA"]

Explanation:

Here, we created an indexed array that contains the name of countries. Here, we used library function json_encode() that will convert the indexed array into a JSON string and assigned to the variable $jsonStr. After that we printed the $jsonStr on webpage using printf() function.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP program to demonstrate the json_encode() funct... >>
<< PHP program to convert an associated array into JS...