php - Print array keys and values -
given table called "people" names of people associated city, need retrieve number of people come same city.
people city john baltimore david london paula baltimore mark baltimore alex london my code follows:
$array_cities = array(); $query = "select * people"; $result = mysql_query($query); if ($result) { while ($record = mysql_fetch_array($result)) $array_cities[] = $record['city']; } print_r(array_count_values($array_cities)); and in browser is:
array ( [baltimore] => 3 [london] => 2 ) the output correct, not graphically good. want like:
baltimore => 3 london => 2 are there other options it?
that's output print_r(). if want else, loop through array print it:
foreach(array_count_values($array_cities) $k => $v) echo $k . " => " . $v . " "; sidenote:
besides print_r() there var_dump() , var_export(), "debugging functions". use them debugging , not printing "nice formatted output".
Comments
Post a Comment