Re: array_multisort into Natural order?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi there! Correct me if I'm wrong but merge() just makes one array of all items. array_multisort() keeps each array separate, while applying the same re-ordering to all three arrays. This allows you to use the same position number to get the required elements from each array, especially where each array is a list of different properties. For example:

$name = array("Joe", "Charlie", "Jack");
$age =  array(30, 12, 66);
$job = array("Plumber", "Student", "Retired");

array_multisort($name, $age, $job); // will re-order all three arrays based on a "regular" alphanumeric sort of the names:

// $name is now array("Charlie", "Jack", "Joe");
// $age is now array(12, 66, 30);
// $job is now array("Student", "Retired", "Plumber", );

// can now do:
$firstName = $name[0]; // returns "Charlie"
$firstAge = $age[0]; // returns Charlie's age 12
$firstJob = $job[0]; // returns Charlie's job "Student"

A merge of these arrays will lose the different types of info each array currently has, and make it impossible to match a name to the age or job.

	Now, my other option is to group the items into a set of arrays:

$array1 = array('name' => 'Joe', age => '30' job => 'Plumber');
$array2 = array('name' => 'Charlie', age => '12' job => 'Student');
$array3 = array('name' => 'Jack', age => '66' job => 'Retired');
$largeArray = array($array1, $array2, $array3);

But, is there a way to the sort $largeArray, based on the "name" value in each individual array? And if so, can that use the natsort()? I don't think an asort() can do either.

Note that the original source of this info is a mySQL db call, and it is initially sorted in the query, but again, there doesn't appear to be a way to natural sort an SQL query either. So I get file1, file10, file2, file20, file5.... instead of file1, file2, file5, file10, file20....

G


----- Original Message -----
From: Jim Lucas <lists@xxxxxxxxx>
Date: Monday, December 13, 2010 16:00
Subject: Re: [PHP] array_multisort into Natural order?
To: George Langley <george.langley@xxxxxxx>
Cc: php-general@xxxxxxxxxxxxx

> On 12/13/2010 11:59 AM, George Langley wrote:
> > Hi all. Can use natsort($array1) to sort a single array of 
> filenames into a "natural "alphanumeric order - 1.php, 2.php, 
> 5.php, 10.php, 20.php, etc.
> > But using array_multisort($array1, $array2, $array3) doesn't 
> offer a natsort option, so I end up with 1.php, 10.php, 2.php, 
> 20.php, 5.php, etc.
> > 
> > Anyone have a solution to this limitation already? Nothing 
> found in the archives or Googling.
> > 	Thanks.
> 
> assuming that you are not using other options of 
> array_multisort(), why not do
> something like this.
> 
> $ar = array_merge($array1, $array2, $array3);
> natsort($ar);
> 
> > 
> > 
> > George Langley    Multimedia 
> Developer    Audio/Video Editor    
> Musician, Arranger, Composer www.georgelangley.ca
> > 
> > 
> > 
> 
> 
> 

George Langley    Multimedia Developer    Audio/Video Editor    Musician, Arranger, Composer www.georgelangley.ca



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux