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

  • SEARCH
  • Home
  • 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 162051
In Process

The Archive Base Latest Questions

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

I have a largish table of data pulled from my database (~1500 rows, each

  • 0

I have a largish table of data pulled from my database (~1500 rows, each with 10-15 fields) and I’m doing a number of filters and generating some stats and storing these in an excel spreadsheet for the user to download.

Rather than hit the database with the same fairly-complicated query over and over with only minor modifications (to the WHERE and ORDER BY), I’m doing one trip to the DB, putting the results into one big array and then using array_filter and array_multisort to get my new views of the data.

I’m new to array_multisort so I’ll post what I’ve done here for critique.

// an numerical array of associative arrays $records = $dbResult->convertToArray();  $fields = $dbResult->getFieldNames();  // this is run once at the start $sortArr = array(); foreach ($fields as $field) $sortArr[$field] = array();  foreach ($records as $r) {     foreach ($r as $key => $value) {         $sortArr[$key][] = $value;     } }  // and then to sort: array_multisort(     $sortArr['Date Completed'], SORT_DESC,     $sortArr['Last Name'], SORT_ASC,     $sortArr['First Name'], SORT_ASC,     $sortArr['Course'], SORT_ASC,     $records ); 

This works fine, though the initial ‘copy the entire result into another array’ seems strange to me. The problem occurs when I need to sort the list again. I have the feeling that my $sortArr needs to stay in sync with the $records array, but that it gets broken after each sort.

I’m not even sure that this is the intended use of array_multisort, so I might be way off track here. Can anyone give some advice or tips? How do you sort multidimensional arrays?

  • 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. 2026-05-11T11:19:21+00:00Added an answer on May 11, 2026 at 11:19 am

    Here’s what I ended up going with. It’s a slightly modified version of a function posted by martin in the comments on the usort page in the PHP manual.

    function arfsort( &$array, $fieldList ){     if (!is_array($fieldList)) {         $fieldList = array(array($fieldList, SORT_ASC));     } else {         for ($i = 0; $i < count($fieldList); ++$i) {             if (is_array($fieldList[$i])) {                 if (!isset($fieldList[$i][1])) $fieldList[$i][1] = SORT_ASC;             } else {                 $fieldList[$i] = array($fieldList[$i], SORT_ASC);             }         }     }     $GLOBALS['__ARFSORT_LIST__'] = $fieldList;     usort( $array, 'arfsort_func' );  }  function arfsort_func( $a, $b ){     foreach( $GLOBALS['__ARFSORT_LIST__'] as $f ) {         $strc = strcasecmp($b[$f[0]], $a[$f[0]]);         if ( $strc != 0 ){             return $strc * (!empty($f[1]) && $f[1] == SORT_DESC ? 1 : -1);         }     }     return 0; } 

    I’ve hopefully made the function a little more robust than the original solution. Usage:

    arfsort($my2DArray, 'id');  // just sort by the id field, ascending // sort by these lastName then firstName, ascending arfsort($my2DArray, array('lastName', 'firstName'));  arfsort($my2DArray, array(     array('date', SORT_DESC),    // sort by date DESC     array('lastName', SORT_ASC), // then by last name ascending     array('firstName'),          // SORT_ASC is the default     'middleInitial'              // and you don't need to wrap stuff in an array. )); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think this will meet your needs: http://www.codeproject.com/KB/cs/winforms2webforms.aspx This will… May 12, 2026 at 12:53 am
  • Editorial Team
    Editorial Team added an answer You can find a port of Rails' Inflector class at… May 12, 2026 at 12:53 am
  • Editorial Team
    Editorial Team added an answer setTimeout() and setInterval() will allow you to execute some code… May 12, 2026 at 12:53 am

Related Questions

I need to import largish (24MB) text files into a MySQL table. Each line
I'm building my first Flex custom component, in Flex 3. It is a data
I have a largish Mercurial repository that I've decided would be better as several
(This question specifically in C#, but applies generally to .NET) I have a largish

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.