0

I have this php code that give data from database and then convert to a json text file, my database name is gengroup_testwebapp and my json file is jsonparsetutorial.txt :

<?php 
// open a connection to mysql
$conn = mysqli_connect("localhost","gengroup_ali","GenGroup$2015","gengroup_testwebapp")
                        or die("Error is : ".mysqli_error($conn));
//fetching data to php 
$sql = "select * from country_info";
$result = mysqli_query($conn , $sql) or die("Error is : ". mysqli_error($conn));
//create an array
$emparray[] = array();
while($row =mysqli_fetch_assoc($result))
{
    $emparray[] = $row;
}

// convert php array to json String and \n
 //write to json file
$fp = fopen('jsonparsetutorial.txt', 'worldpopulation');
fwrite($fp, json_encode($emparray));
fclose($fp); ?>

I wanna convert this code to json array file. how can I convert its data to a json file like this:

{ "worldpopulation": 
[
     {
     "rank":1,"country":"China",
     "population":"1,354,040,000",
     "flag":"http://www.gengroup.ir/testwebapp/flag/china.png"
     }, 

     {
     "rank":2,"country":"India",
     "population":"1,210,193,422",
     "flag":"http://www.gengroup.ir/testwebapp/flag/india.png"
     }, 

     {
     "rank":3,"country":"United States",
     "population":"315,761,000",
     "flag":"http://www.gengroup.ir/testwebapp/flag/unitedstates.png"
     }, 

     {
     "rank":4,"country":"Indonesia",
     "population":"237,641,326",
     "flag":"http://www.gengroup.ir/testwebapp/flag/indonesia.png"
     }, 

     {
     "rank":5,"country":"Brazil",
     "population":"193,946,886",
     "flag":"http://www.gengroup.ir/testwebapp/flag/brazil.png"
     }, 

     {
     "rank":6,"country":"Pakistan",
     "population":"182,912,000",
     "flag":"http://www.gengroup.ir/testwebapp/flag/pakistan.png"
     }, 

     {
     "rank":7,"country":"Nigeria",
     "population":"170,901,000",
     "flag":"http://www.gengroup.ir/testwebapp/flag/nigeria.png"
     }, 

     {
     "rank":8,"country":"Bangladesh",
     "population":"152,518,015",
     "flag":"http://www.gengroup.ir/testwebapp/flag/bangladesh.png"
     }, 

     {
     "rank":9,"country":"Russia",
     "population":"143,369,806",
     "flag":"http://www.gengroup.ir/testwebapp/flag/russia.png"
     }, 

     {
     "rank":10,"country":"Japan",
     "population":"127,360,000",
     "flag":"http://www.gengroup.ir/testwebapp/flag/japan.png"
     } 
] }
1
  • 1
    Can you please post how your original data looks like? Commented Sep 6, 2015 at 5:52

1 Answer 1

1

Simply do this.

$arr['worldpopulation']= $emparray[];
$fp = fopen('jsonparsetutorial.txt', "w");
fwrite($fp, json_encode($arr));
fclose($fp); ?>

Don't forget to mention mode of open like w for write.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.