ADVERTISEMENT
ADVERTISEMENT

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

Here, we are going to demonstrate the json_encode() function with indexed array of strings in PHP.
Submitted by Nidhi, on November 23, 2020

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.

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.

PHP JSON Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.