...

View Full Version : Sorting an array of arrays by the value of a certain key in side an array in the main



Dubz
01-16-2012, 12:10 AM
This may sound confusing so lets try to make it simpler with layout :P

Lets say i have an array like this:


$myBigArray = array(
[0] => array('name' => 'Bob', 'city' => 'san francisco', 'state' => 'california'),
[1] => array('name' => 'Joe', 'city' => 'new york city', 'state' => 'new york'),
[2] => array('name' => 'Jim', 'city' => 'westfield', 'state' => 'new york'),
[3] => array('name' => 'Mary', 'city' => 'carson city', 'state' => 'california'),
[4] => array('name' => 'Sam', 'city' => 'paterson', 'state' => 'new york'),
);


Now if you were to print_r this out, it would be something like this:


array(
[0] => array(
name => Bob
city => san francisco
state => california
)
[1] => array(
name => Joe
city => new york city
state => new york
)
[2] => array(
name => Jim
city => westfield
state => new york
)
[3] => array(
name => Mary
city => carson city
state => california
)
[4] => array(
name => Sam
city => paterson
state => new york
)
)


My question is, how can i resort this based on a certain key in this? Say we want to group them by the states alphabetically. The result I want is this:


array(
[0] => array(
name => Bob
city => san francisco
state => california
)
[1] => array(
name => Mary
city => carson city
state => california
)
[2] => array(
name => Joe
city => new york city
state => new york
)
[3] => array(
name => Jim
city => westfield
state => new york
)
[4] => array(
name => Sam
city => paterson
state => new york
)
)

Yes i want the array numbers to change also. I tried various ways of asort , ksort, etc but didnt seem to do it right unless i did it wrong.

Spookster
01-16-2012, 12:30 AM
You are sorting a multidimensional array so did you try using the multisort function?
http://www.php.net/manual/en/function.array-multisort.php

Spookster
01-16-2012, 12:51 AM
<?php

$myBigArray = array(
array('name' => 'Bob', 'city' => 'san francisco', 'state' => 'california'),
array('name' => 'Joe', 'city' => 'new york city', 'state' => 'new york'),
array('name' => 'Jim', 'city' => 'westfield', 'state' => 'new york'),
array('name' => 'Mary', 'city' => 'carson city', 'state' => 'california'),
array('name' => 'Sam', 'city' => 'paterson', 'state' => 'new york'),
);
var_dump($myBigArray);

// Grab list of states to sort by
foreach($myBigArray as $record) {
$sortColumn[] = $record['state'];
}

// Sort by states
array_multisort($sortColumn, SORT_ASC, $myBigArray);
var_dump($myBigArray);
?>


Results:


array
0 =>
array
'name' => string 'Bob' (length=3)
'city' => string 'san francisco' (length=13)
'state' => string 'california' (length=10)
1 =>
array
'name' => string 'Joe' (length=3)
'city' => string 'new york city' (length=13)
'state' => string 'new york' (length=8)
2 =>
array
'name' => string 'Jim' (length=3)
'city' => string 'westfield' (length=9)
'state' => string 'new york' (length=8)
3 =>
array
'name' => string 'Mary' (length=4)
'city' => string 'carson city' (length=11)
'state' => string 'california' (length=10)
4 =>
array
'name' => string 'Sam' (length=3)
'city' => string 'paterson' (length=8)
'state' => string 'new york' (length=8)

array
0 =>
array
'name' => string 'Bob' (length=3)
'city' => string 'san francisco' (length=13)
'state' => string 'california' (length=10)
1 =>
array
'name' => string 'Mary' (length=4)
'city' => string 'carson city' (length=11)
'state' => string 'california' (length=10)
2 =>
array
'name' => string 'Jim' (length=3)
'city' => string 'westfield' (length=9)
'state' => string 'new york' (length=8)
3 =>
array
'name' => string 'Joe' (length=3)
'city' => string 'new york city' (length=13)
'state' => string 'new york' (length=8)
4 =>
array
'name' => string 'Sam' (length=3)
'city' => string 'paterson' (length=8)
'state' => string 'new york' (length=8)

Dubz
01-16-2012, 01:03 AM
// Grab list of states to sort by
foreach($myBigArray as $record) {
$sortColumn[] = $record['state'];
}

// Sort by states
array_multisort($sortColumn, SORT_ASC, $myBigArray);
?>



Just what I needed. Thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum