I need some help understanding how to utilize fgetcsv and arrays to manipulate data. I am confused on how to relate the data for multisort to achieve an "ORDER BY $lastNameArray" function without mixing up the data. I sorted ONE of the array elements fine, but the other "array 2" was still the same, leaving it mixed up. How do I properly sort the data, and maintain the data relationship with the other columns on the row so that I can iterate through it and manipulate, echo, print etc.
Error Message:
Warning: array_multisort() [function.array-multisort]: Argument 2 is expected to be an array or a sort flag in C:\wamp\www\Codelobster2.php on line 42
Code that correctly works to loop through csv and show phone number results in a table. This does NOT sort. Besides the sorting, it accomplishes exactly what I need.
<?php
//table setup
//print_r($associativeAddressArray);
//header setup for table output view
echo "<h1>Phone Directory</h1>";
echo "<table border = '1' width = '50%'>\n";
echo "<tr>";
echo "<th>Name</th>";
echo "<th>Phone Number</th>";
//get data from file
$fileName = 'phoneData.csv';
$file = fopen($fileName,"r");
//while not to end of file
while (!feof开发者_如何转开发($file) )
{
while (($csv_line = fgetcsv($file)) !== FALSE)
{
//print_r($csv_line);
echo "<tr>"; //beginning new row of record
echo "<td>" . $csv_line[0] .", ". $csv_line[1]. "</td>"; // 0=lastName, 1=FirstName
echo "<td>" . $csv_line[5] ."-" . $csv_line[6]. "</td>"; //
echo "</tr>"; //new row
}
echo "</table>\n";
}
echo '<FORM><INPUT TYPE="button" VALUE="Back" onClick="history.go(-1);return true;"></FORM>'; //let me know if a better way to do this exists, I'm very limited on html experince
?>
As a further edit for other students, the final loop to reference the array worked when I did:
//array_multisort($names, $data); // need to sort the data by last name while keeping the seperate arrays in sync
array_multisort($names, SORT_DESC, $phones, SORT_DESC, $data);
//Obtain a list of columns
foreach ($data as $record)
{
echo "<tr>"; //beginning new row of record
echo "<td>" . $record['name'] . "</td>"; // 0=lastName, 1=FirstName
echo "<td>" . $record['phone'] . "</td>"; //
echo "</tr>"; //new row
}
If you want to sort this using php's array sorting functions then you need to build up a full array, you won't be able to do it as you read from the file.
So instead of looping through and printing out while each line is read, you need to build an array of all the data.
Assuming they only need to be sorted by Name or Phone Number:
$data = array();
while (($csv_line = fgetcsv($file)) !== FALSE) {
$name = $csv_line[0] .", ". $csv_line[1];
$phone = $csv_line[5] ."-" . $csv_line[6];
$data[] = array(
'name' => $name,
'phone' => $phone
);
$phones[] = $phone;
$names[] = $name;
}
You also don't need to wrap that while loop within the feof while loop, fgetcsv will return false on error (including EOF).
Once you have your $data array, you can use array_multisort (as shown in Example 3):
array_multisort($names, SORT_DESC, $phones, SORT_DESC, $data);
This could fail for large files as all the data needs to be stored in memory, utilizing a database to store data and return sorted data is a much more efficient option.
Also depending on HOW MUCH DATA you are importing, you might get better milage doing an import of your CSV data into a relational database. Then running your sort or other functions via the DB.
Again it really depends on traffic, you could get away with sufficient sorting using the array_multisort()
.
精彩评论