We help IT Professionals succeed at work.

Array Column Sort not working in PHP 5.2.6

rschroll asked
Last Modified: 2012-06-27
I had to rebuild a server due to a disk crash.  The old server was running PHP 5.0.5, Fedora Core 4, Apache 2.0.  The new server has PHP 5.2.6, Fedora 8, and Apache 2.0.

The following sort code (not my code) I've used for years now does not run on PHP 5.2.6.

The resulting output is the original unsorted array.

Are there any PHP.ini settings that I might have missed that could affect this?

array_multisort by itself will work and sort the array, but the function arrayColumnSort eliminates the need to determine the column keys everytime I want to use array_multisort.

Thanks in advance for any help!
   //Multidimension Associative Array Sort Function
  function arrayColumnSort()
   $n = func_num_args();
   $ar = func_get_arg($n-1);
     return false;
   for($i = 0; $i < $n-1; $i++)
     $col[$i] = func_get_arg($i);
   foreach($ar as $key => $val)
     foreach($col as $kkey => $vval)
         ${"subar$kkey"}[$key] = $val[$vval];
   $arv = array();
   foreach($col as $key => $val)
     $arv[] = (is_string($val) ? ${"subar$key"} : $val);
   $arv[] = $ar;
   call_user_func_array("array_multisort", $arv);
   return $ar;
// load example data in array
$data2[1]["year"] = 2004;
$data2[1]["plant_id"] = 3;
$data2[1]["plant_desc"] = 'Plant 2';
$data2[1]["count"] = 246;
$data2[2]["year"] = 2004;
$data2[2]["plant_id"] = 4;
$data2[2]["plant_desc"] = 'Plant 6';
$data2[2]["count"] = 23;	
$data2[3]["year"] = 2004;
$data2[3]["plant_id"] = 59;
$data2[3]["plant_desc"] = 'Colorado Plant';
$data2[3]["count"] = 13;	
$data2[4]["year"] = 2004;
$data2[4]["plant_id"] = 100;
$data2[4]["plant_desc"] = 'Corporate Office';
$data2[4]["count"] = 32;	
$data2[5]["year"] = 2004;
$data2[5]["plant_id"] = 205;
$data2[5]["plant_desc"] = 'South Plant';
$data2[5]["count"] = 53;	
$data2[6]["year"] = 2005;
$data2[6]["plant_id"] = 3;
$data2[6]["plant_desc"] = 'Plant 2';
$data2[6]["count"] = 246;
$data2[7]["year"] = 2005;
$data2[7]["plant_id"] = 4;
$data2[7]["plant_desc"] = 'Plant 6';
$data2[7]["count"] = 23;	
$data2[8]["year"] = 2005;
$data2[8]["plant_id"] = 59;
$data2[8]["plant_desc"] = 'Colorado Plant';
$data2[8]["count"] = 13;	
$data2[9]["year"] = 2005;
$data2[9]["plant_id"] = 100;
$data2[9]["plant_desc"] = 'Corporate Office';
$data2[9]["count"] = 32;	
$data2[10]["year"] = 2005;
$data2[10]["plant_id"] = 205;
$data2[10]["plant_desc"] = 'South Plant';
$data2[10]["count"] = 53;	
//sort data array by plant_desc, year
$srt_data2 = arrayColumnSort("plant_desc", SORT_ASC, SORT_STRING, "year", SORT_ASC, SORT_NUMERIC, $data2);

Open in new window

Watch Question

Top Expert 2007
This one is on us!
(Get your first solution completely free - no credit card required)


Thanks hernst42, that solved the problem.

Can you briefly tell me why this reference works since $ar is populated within the function?  Did something change from php 5.0.x to 5.2.x.

Thanks for your help, truly appreciated.
Top Expert 2007

No can't tell you why, should have been so since ever. Maybe a bug in 5.0

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.