Saturday, 15 August 2015

php - Count unique value from associative array -



php - Count unique value from associative array -

first, help.

i've spent countless hours on here , other forums trying find exact solution either 1) i'm not understanding one's i've read or 2)i haven't found right answer.

in php, i've run complex query returns set of records similar to:

id | name | direction| 1 aaa east 2 bbb west 3 ccc east

i've created associative array such as:

$query=("select * foo"); $result=mysql_query($query); $array=mysql_fetch_assoc($result);

now, need seems simple i'm not grasping concept reason. need loop through entire $array , homecoming count of value want specify , store count in variable.

i.e. show me how many times east shows in "direction" column , set in variable called $eastcount.

i've tried various combinations of using foreach loops incremental counts , have tried using array_count_values have not been able set pieces :/

// build query $query=("select * foo"); // execute query $result=mysql_query($query); // declare vars $east_count = 0; // iterate through results while ($data = mysql_fetch_array($result)) { // grab direction column value $direction = $data['direction']; // observe 'east' if ($direction == 'east') { // increment 'east' count $east_count++; } } // print # of times had 'east' echo("direction said 'east' $east_count times");

php mysql arrays

No comments:

Post a Comment