sorting a multidimesional array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mBull
Forum Newbie
Posts: 5
Joined: Fri Feb 19, 2010 2:47 pm

sorting a multidimesional array

Post by mBull »

Hi,

I want to sort on the population, but want to keep the values for each city next to each other

Code: Select all

<?php
$city = array("New York" => array("NY" => 8008278),
				    "Los Angeles" => array ("CA" => 369480),
					"Chicago" => array ("IL" => 2896016),
					"Houston" => array ("TX" => 1953631),
					"Philadelphia" => array ("PA" => 1517550),
					"Phoenix" => array ("AZ" => 1321045),
					"San Diego" => array ("CA" => 1223400),
					"Dallas" => array ("TX" => 1188580),
					"San Antonio" => array ("TX" => 1144646),
					"Detroit" => array ("MI" => 951270));
print ("<br>without sorting<br>");
//print array to screen
foreach ($city AS $name => $tags){
	foreach ($tags as $states => $population) {
		print "name: ".$name."  tags: " .$states. " populatie: " .$population. "<br>";
	}
}
print ("<br> sorting on the key value city<br>");
//sort on key value's of $city ($name)
ksort($city, SORT_ASC);
//printing the array to the screen
foreach ($city AS $name => $tags){
	foreach ($tags as $states => $population) {
		print "name: ".$name."  tags: " .$states. " populatie: " .$population. "<br>";
	}
}
print ("<br> sorting on the tags / population<br>");
// this is where it goes wrong I assume that $tags is the array wich it is, and the key value are the states names, but it doesn't work. 
ksort($tags, SORT_ASC);

//printing the array to the screen
foreach ($city AS $name => $tags){
	foreach ($tags as $states => $population) {
		print "name: ".$name."  tags: " .$states. " populatie: " .$population. "<br>";
	}
}

?>
Last edited by mBull on Wed Oct 13, 2010 12:39 pm, edited 3 times in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: sorting a multidimesional array

Post by pickle »

usort() or array_multisort() might work.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
mBull
Forum Newbie
Posts: 5
Joined: Fri Feb 19, 2010 2:47 pm

Re: sorting a multidimesional array

Post by mBull »

Wel I just can't get it to work, i can sort the array on the city names, but not it's tages (wich are the states) neither on the population

I tried with the multy sort but when i printed it out or used var dump it never is the wright order they just look a bit scrambled

any help?

thanks for the hints already.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: sorting a multidimesional array

Post by AbraCadaver »

Code: Select all

foreach ($city as $key => $row) {
    $population[$key] = current($row);
}
array_multisort($population, $city);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kalpesh.mahida
Forum Commoner
Posts: 36
Joined: Wed Oct 06, 2010 7:09 am

Re: sorting a multidimesional array

Post by kalpesh.mahida »

Hi,

mBull try to define your array in a different way as i did in bellow snippet and make a use of asort() to achieve desired result.

Code: Select all

$arrCityPopulation['city'] = array('New York','Los Angeles','Chicago','Houston');
$arrCityPopulation['code'] = array('NY','CA','IL','TX');
$arrCityPopulation['population'] = array(8008278,369480,2896016,1953631);

echo '<pre>';
print_r($arrCityPopulation);

echo '<br>';echo '<br>';
asort($arrCityPopulation['population'],SORT_NUMERIC);

echo 
'<table>
<tr>
    <td>City</td>
    <td>Code</td>
    <td>Population</td>
</tr>';
foreach($arrCityPopulation['population'] as $key=>$value) 
{
    echo "<tr>";
    echo "<td>{$arrCityPopulation['city'][$key]}</td><td>{$arrCityPopulation['code'][$key]} </td><td>{$value}</td>";
    echo "</tr>";
}
echo '</table>';
Hope this will help you.

Kalpesh Mahida
mBull
Forum Newbie
Posts: 5
Joined: Fri Feb 19, 2010 2:47 pm

Re: sorting a multidimesional array

Post by mBull »

thanks for the help. it realy helped me, cheers
Post Reply