1

我目前有一个带有用户条目 TABLE 的数据库 - 我在其中查询如下内容:

user_id: 1
user_point: 20

user_id: 1
user_point: 50

user_id: 2
user_point: 10

该数据被保存到一个数组中,如下所示:

$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. 如何将我的数组保存在我基于 GROUP 条目的位置user_id,这样它user_id = 1只会在数组中出现一次并加上相关的user_point- 在这种情况下等于“70 点”?

  2. 之后我如何对数组进行排序,以便user_id数量最多的user_point将在数组中排在第一位?

对于问题 1 - 我想做类似 for 循环的事情:

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

对于问题 2 - 查看了usort() - 但不完全确定如何在这种情况下使用它

任何帮助,示例将不胜感激!

4

1 回答 1

1

使用它:由于时间限制,我无法详细描述

<?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 as $array) {
  //check if user_id = user_id - then perhaps save into a 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 as $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
于 2013-09-04T16:40:36.200 回答