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
mBull
Forum Newbie
Posts: 5 Joined: Fri Feb 19, 2010 2:47 pm
Post
by mBull » Tue Oct 12, 2010 8:16 am
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.
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Tue Oct 12, 2010 9:46 am
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
Post
by mBull » Wed Oct 13, 2010 12:34 pm
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.
AbraCadaver
DevNet Master
Posts: 2572 Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:
Post
by AbraCadaver » Wed Oct 13, 2010 1:09 pm
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
Post
by kalpesh.mahida » Thu Oct 14, 2010 1:40 am
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
Post
by mBull » Mon Oct 18, 2010 1:55 pm
thanks for the help. it realy helped me, cheers