Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4029362
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:20:09+00:00 2026-05-20T11:20:09+00:00

I need some help understanding how to utilize fgetcsv and arrays to manipulate data.

  • 0

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

        } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:20:09+00:00Added an answer on May 20, 2026 at 11:20 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help understanding some C++ operator overload statements. The class is declared like
I am getting a little confused and need some help please. Take these two
Need some help understanding why my concat() is failing and how to fix it.
I need some help understanding this bit of code pre { white-space: pre; white-space:
I need some help understanding some of the points from Paul Graham’s What Made
I need some help understanding what's happening here. This code is from a models/log.py
I need some help understanding the correct way to mix variables with strings like
I need some help understanding how stdext::hash_multimap's lower_bound, upper_bound and equal_range work (at least
I think I need some help understanding how static objects persist in an ASP.Net
I'm new to ORM stuff and I need some help understanding something. Let's assume

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.