Jump to content

Sort array


jandrews3

Recommended Posts

I have a multidimensional array called $data with 2 keys: name and member_no. I want to sort the array by name. Is there a simple way to do this. I don't understand anything I'm seeing on php.net. There are too many references to sorting, and too many different types of arrays which can be sorted. I'm not understanding it. It seems to me there's gotta be a simple way to do this. THANK YOU!

Link to comment
https://forums.phpfreaks.com/topic/127331-sort-array/
Share on other sites

Sure.

 

Array ( [] => Array ( [name] => Emeyese, Akpodi Ogaga [id] => 3441 ) [1] => Array ( [name] => Edge, Timothy [id] => 1439 ) [2] => Array ( [name] => Deka, Surajueet [id] => 3659 ) [3] => Array ( [name] => de Ribaucourt, Humbert [id] => 2455 ) [4] => Array ( [name] => Davie, James [id] => 3419 ) [5] => Array ( [name] => Cutlip, Don [id] => 2896 ) [6] => Array ( [name] => Crow, Thomas [id] => 1092 ) [7] => Array ( [name] => Corderman, John [id] => 3520 ) [8] => Array ( [name] => Converse, Kent [id] => 1400 ) [9] => Array ( [name] => Connell, David [id] => 660 ) [10] => Array ( [name] => Collins, Jr., Frank [id] => 599 ) [11] => Array ( [name] => Clutton, John [id] => 3150 ) [12] => Array ( [name] => Cluppert, Charlene [id] => 3808 ) [13] => Array ( [name] => Cline, Thomas [id] => 3714 ) [14] => Array ( [name] => Bowers, Helen [id] => 3429 ) [15] => Array ( [name] => Bora, Anil [id] => 3249 ) [16] => Array ( [name] => Boberg, Anne-Marit [id] => 2845 ) [17] => Array ( [name] => Blankenzee, Max [id] => 3962 ) [18] => Array ( [name] => Blackwell, David [id] => 1990 ) [19] => Array ( [name] => Ahuja, Yog Raj [id] => 3550 ) [20] => Array ( [name] => Abdul Aziz, Asim [id] => 3544 ) [21] => Array ( [name] => Adair, Robin [id] => 1049 ) [22] => Array ( [name] => Adeniran, lasun [id] => 1557 ) [23] => Array ( [name] => Adeyemo, Victoria Adetoun [id] => 3797 ) [24] => Array ( [name] => Agnew, Bob [id] => 1133 ) [25] => Array ( [name] => Agrawal, Deepak [id] => 3180 ) [26] => Array ( [name] => Evans, Stephen [id] => 3563 ) [27] => Array ( [name] => Fields, Troy [id] => 421 ) [28] => Array ( [name] => Finck, Frank [id] => 3370 ) [29] => Array ( [name] => Gandhi, Parimal [id] => 704 ) [30] => Array ( [name] => Gardberg, Jerome [id] => 1876 ) [31] => Array ( [name] => GIUSTI, ANDRE LUIZ [id] => 3896 ) [32] => Array ( [name] => Hammond, David [id] => 3768 ) [33] => Array ( [name] => Imes, Sharon [id] => 3790 ) [34] => Array ( [name] => Jenkins, Don [id] => 3343 ) [35] => Array ( [name] => Jennings, Bruce [id] => 539 ) [36] => Array ( [name] => Jeschofnig, Peter [id] => 2234 ) [37] => Array ( [name] => Joghee Gowder, Ravi [id] => 3718 ) [38] => Array ( [name] => Johnson, Dale [id] => 3065 ) [39] => Array ( [name] => Kharbanda, Narender [id] => 3336 ) [40] => Array ( [name] => Kiedaisch, Ed [id] => 3434 ) [41] => Array ( [name] => King, Ted [id] => 2043 ) [42] => Array ( [name] => McCarthy, Barbara [id] => 3658 ) [43] => Array ( [name] => McCloy, Peter [id] => 2929 ) [44] => Array ( [name] => McGennisken, Annie [id] => 3018 ) [45] => Array ( [name] => Mullett, Ted [id] => 137 ) [46] => Array ( [name] => Oladimeji, Femi [id] => 3384 ) [47] => Array ( [name] => Rash, Wayne [id] => 3929 ) [48] => Array ( [name] => Rasmusson, William [id] => 3301 ) [49] => Array ( [name] => Roberts, James [id] => 3840 ) [50] => Array ( [name] => Sydes, Bob [id] => 2766 ) [51] => Array ( [name] => Talbot, Arthur [id] => 3828 ) [52] => Array ( [name] => Talbot, Melba [id] => 3581 ) [53] => Array ( [name] => Tapuska, Bill [id] => 1287 ) [54] => Array ( [name] => Tatlow, Gary [id] => 3691 ) [55] => Array ( [name] => Thaha, Mohamed Nisthar [id] => 2716 ) [56] => Array ( [name] => Wilkins, Mandy [id] => 3034 ) [57] => Array ( [name] => Williams, Henry [id] => 3209 ) [58] => Array ( [name] => Williams, Mike [id] => 2930 ) [59] => Array ( [name] => Williams, Oladipo [id] => 2790 ) [60] => Array ( [name] => Yoshida, Steve K. [id] => 442 ) [61] => Array ( [name] => Young, Herbert R. [id] => 254 ) [62] => Array ( [name] => Zdrazil, Alfred [id] => 3325 ) [63] => Array ( [name] => Zilliox, Larry [id] => 3373 ) [64] => Array ( [name] => Zweck, Carolyn [id] => 3908 ) )

 

Link to comment
https://forums.phpfreaks.com/topic/127331-sort-array/#findComment-658683
Share on other sites

The array is created from data from two different tables.

			$queryg = "SELECT * FROM ithf_groups WHERE title = '$title'";
			$resultg= mysql_query($queryg) or die ("Could not perform query: ".mysql_error());
			while ($rowg = mysql_fetch_array($resultg)){

				$ident = $rowg['member_no'];

				$queryp = "SELECT * FROM ithf_members WHERE id = '$ident'";
				$resultp= mysql_query($queryp) or die("Could not perform query: ".mysql_error());
				$rowp = mysql_fetch_array($resultp);

				$data[$count] = array('name' => $rowp['lname'].", ".$rowp['fname'], 'id' => $rowg['member_no']);
				$count++;
			}

 

 

Link to comment
https://forums.phpfreaks.com/topic/127331-sort-array/#findComment-658686
Share on other sites

I would just do something like this:

$array would be the original array, just replace $array with your array variable name.

 

<?php
//$array is the original array
$sortarray = array();
foreach($array as $key=$value)
{
$sortarray[$value['id']] = $value['name'];
}
asort($sortarray);
$array = array();

$counter = 0;
foreach($sortarray as $key=>$value)
{
$array[$counter]['name'] = $value;
$array[$counter]['id'] = $key;
    $counter++;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/127331-sort-array/#findComment-658690
Share on other sites

oops, you're right.  try this

<?php
//$array is the original array
$sortarray = array();
foreach($array as $key=>$value)
{
   $sortarray[$value['id']] = $value['name'];
}
asort($sortarray);
$array = array();

$counter = 0;
foreach($sortarray as $key=>$value)
{
   $array[$counter]['name'] = $value;
   $array[$counter]['id'] = $key;
    $counter++;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/127331-sort-array/#findComment-658697
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.