php - Save DB query into an array and group all entries of user_id into one array entry -


i have database user entry table - query out like:

user_id: 1 user_point: 20  user_id: 1 user_point: 50  user_id: 2 user_point: 10 

this data saved array, looks this:

$highscoredata = array();  while ($row = mysql_fetch_assoc($res)) {   $highscoredata []= (object) array(      "user_id" => $row['user_id'],      "username" => $row['username'],      "user_firstname" => $row['user_firstname'],      "user_lastname" => $row['user_lastname'],      "user_point" => $row['user_point']   ); } $feedback = $highscoredata; 
  1. how save array group entries based on user_id , that user_id = 1 appear once in array related added user_point - in case equal "70 points"?

  2. and how afterwards sort array, user_id highest number of user_point line first in array?

for question 1 - figured loop:

for( var = 0; < highscoredata.length; i++ ){   //check if user_id = user_id - perhaps save new array? } 

for question 2 - looked @ usort() - not totally sure how use in context

any help, examples appreciated!

use it: not describe in detail time limitation

<?php  $highscoredata []= (object) array(      "user_id" => 1,      "username" => 'name1',      "user_firstname" => "first name 1",      "user_lastname" => "last name 1",      "user_point" => 20   );   $highscoredata []= (object) array(      "user_id" => 1,      "username" => 'name1',      "user_firstname" => "first name 1",      "user_lastname" => "last name 1",      "user_point" => 50   );  $highscoredata []= (object) array(      "user_id" => 2,      "username" => 'name2',      "user_firstname" => "first name 2",      "user_lastname" => "last name 2",      "user_point" => 80   );  $user_point_sum = array(); $users = array(); foreach($highscoredata $array) {   //check if user_id = user_id - perhaps save new array?   $user_id = $array->user_id;   $username = $array->username;   $user_firstname = $array->user_firstname;   $user_lastname = $array->user_lastname;   $user_point = $array->user_point;    if(!isset($user_point_sum[$user_id])) {       $user_point_sum[$user_id] = 0;       $user_info = new stdclass();      $user_info->user_id = $user_id;      $user_info->username = $username;      $user_info->user_firstname = $user_firstname;      $user_info->user_lastname = $user_lastname;      $users[$user_id] = $user_info;   }   $user_point_sum[$user_id] += $user_point; }  arsort($user_point_sum);  $array_object = array(); foreach($user_point_sum $user_id=>$user_point) {      $object = new stdclass();      $object->user_id = $user_id;     $object->username = $users[$user_id]->username;     $object->user_firstname = $users[$user_id]->user_firstname;     $object->user_lastname = $users[$user_id]->user_lastname;     $object->user_point = $user_point;      $array_object[] = $object; }  var_dump($array_object);  ?>  output:  array   0 =>      object(stdclass)[6]       public 'user_id' => int 2       public 'username' => string 'name2' (length=5)       public 'user_firstname' => string 'first name 2' (length=12)       public 'user_lastname' => string 'last name 2' (length=11)       public 'user_point' => int 80   1 =>      object(stdclass)[7]       public 'user_id' => int 1       public 'username' => string 'name1' (length=5)       public 'user_firstname' => string 'first name 1' (length=12)       public 'user_lastname' => string 'last name 1' (length=11)       public 'user_point' => int 70 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -