Question

Read a file called populations.txt and populate a php associative array with the values read from...

Read a file called populations.txt and populate a php associative array with the values read from the file.
Here is an example populations.txt file:
Dhaka, 23 million
Bangaluru, 21 million
New York, 9 million
Emporia, 23 thousand
Here's some pseudo code:
open the file
loop through the lines in the file
read a line into a string variable
explode (or is it split, or something else) the string into key value pairs
save the key value pair into your associative array
print a table header
loop through the keys of your associative array
print each key value pair


Then move the file and write to another file and read that file in the reverse order Using sprintf
like this

23 million, Dhaka
21 million, Bangaluru
9 million , New York

Using sprintf

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

INPUT File:

populations.txt

Dhaka, 23 million

Bangaluru, 21 million

New York, 9 million

Emporia, 23 thousand

CODE: population.php

<html>
<head>
<style>
table
{
border-style:solid;
border-width:1px;
border-color:blue;
}
</style>
</head>
<body bgcolor="#EEFDEF">
<?php
$filename = 'populations.txt';
$contents = file($filename);

$data =array();
foreach($contents as $line) {
$data[explode(",",$line)[0]]=explode(",",$line)[1];
}

echo "<table border='1'><tr><th>Key</th><th>Value</th></tr>";
foreach($data as $x => $x_value) {
echo "<tr><td>" . $x . "</td><td>" . $x_value."</td></tr>";
}
echo "</table>";

echo "</br></br>Reading on Reverse Using sprintf</br>";
foreach($data as $x => $x_value) {
$txt = sprintf("%s, %s.",$x_value,$x);
echo $txt."</br>";
}
?>
</body>
</html>

Add a comment
Know the answer?
Add Answer to:
Read a file called populations.txt and populate a php associative array with the values read from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Print out information of PHP use phpinfo() function. 2. Write a program that check and...

    1. Print out information of PHP use phpinfo() function. 2. Write a program that check and print odd / even numbers (from number 1 to 100 using for/while loop). Display the results within an HTML table with 2 columns as shown below: NUMBERS RESULTS 1 ODD 2 EVEN 3 ODD HINT: use <table> tags to create a table, <th> tags for ‘Numbers’ and ‘Results’. Wrap code PHP inside HTML code. For example: ​<html> ​​<title>CHECK ODD or EVEN</title> ​​<body> ​​​<table> ​​​​<?php...

  • I am having problems with reading a file into an array. This is my code. This...

    I am having problems with reading a file into an array. This is my code. This is what I get when I run my program. But this is my text file I am reading. I tried everything and it seems to be reading in the last digit of the file. I want to read in their names line by line into an array and ultimatly also read in the scores line by line. 1 E/7 Programming Assignment 6.cpp Defines the...

  • This lab will combine reading data from a file and searching the array to find a...

    This lab will combine reading data from a file and searching the array to find a specific value. Assignment Write a program that reads in a file full of strings into an array, and prompts the user for a string to find in the array. The program should loop until a sentinel value (such as -1) is entered. After looping in main() for the input, write a search function, with the following prototype: int findWord(string [], int, string); with arguments...

  • For your second program, please read the data from the input file directly into an array....

    For your second program, please read the data from the input file directly into an array. (You may safely dimension your array to size 300.) Then close the input file. All subsequent processing will be done on the array. Your program should have two functions besides main(). The first function will print out the contents of the array in forward order, 10 numbers per line, each number right justified in a 5 byte field. The second function will print out...

  • <html> <!--This is the form for an instructor to upload a file having the grade of...

    <html> <!--This is the form for an instructor to upload a file having the grade of students in a class--> <body> <h2>Upload Student Grade</h2> <form action="1.php" method="POST" enctype="multipart/form-data">    Instructor's First Name:<input type="text" name="first"><br><br>    Instructor's Last Name: <input type="text" name="last"><br><br>      Instructor's Department Name:<input type="text" name="department"><br><br>          Upload student grade: <input type="file" name="image" /><br><br> <input type="submit"/> </form>    </body> </html> //***************************************************Q1****************************************************************************      /*(((( Q1)))). (18 points) Add the code below that *   uploads the file containing the students'...

  • Objective: Learn how to -- read string from a file -- write multiple files Problem: Modify...

    Objective: Learn how to -- read string from a file -- write multiple files Problem: Modify the Person class in Lab #4. It has four private data members firstNam (char[20]), day, month, year (all int); and two public member functions: setPerson(char *, int, int, int) and printInfo(). The function setPerson sets the first name and birthday in the order of day, month and year. The function printInfo prints first name and birthday. But it should print the date in the...

  • write a C++ program that reads the gallery's inventory from a file called ArtFile.txt or ArtFile.csv...

    write a C++ program that reads the gallery's inventory from a file called ArtFile.txt or ArtFile.csv into the array of artist struct. Then allow the user to look up the art by specifying any field in the record. [The file extracted a selected number of art works from the National Gallery of Art: NGS.gov.] The user should be able to specify a field, and a value for that field, and the program then returns all artworks that match those criteria....

  • Read person information in file person.txt and store to array p using DYNAMIC MEMORY. Set the...

    Read person information in file person.txt and store to array p using DYNAMIC MEMORY. Set the spouse pointer for each person to NULL value first. Perform marry operation for Mary and Tom. You can marry two person by setting their spouse pointer to point to each other (store address of one another). You need to go through the array to find Mary and Tom by comparing name using strcmp. Print out the content in array p where you need to...

  • HOW TO FiX EXCEPTIONS??? In order to populate the array, you will need to split() each...

    HOW TO FiX EXCEPTIONS??? In order to populate the array, you will need to split() each String on the (,) character so you can access each individual value of data. Based on the first value (e.g., #) your method will know whether to create a new Manager, HourlyWorker, or CommissionWorker object. Once created, populate the object with the remaining values then store it in the array. Finally, iterate through the array of employees using the enhanced for loop syntax, and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT