dongxian3852 2014-10-24 17:05
浏览 41

在php中改变数组排序代码

First i show the code, for help me, and in the end i will explain what i need. Database

CREATE TABLE IF NOT EXISTS `tickets` (
      `ticketid` int(10) NOT NULL AUTO_INCREMENT,
      `datetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
      `game` enum('1','2','3') NOT NULL DEFAULT '1',
      `gameid` int(100) NOT NULL,
      `userid` int(11) NOT NULL DEFAULT '0',
      `sum` int(11) NOT NULL,
      `username` varchar(50) NOT NULL,
      `numbers` varchar(27) NOT NULL,
      `count` int(2) NOT NULL,
      `how_much_win` int(20) NOT NULL,
      `win` enum('yes','no') NOT NULL DEFAULT 'no',
      `checked` enum('yes','no') NOT NULL DEFAULT 'no',
      PRIMARY KEY (`ticketid`)
    ) ENGINE=MyISAM;

INSERT INTO `tickets` (`ticketid`, `datetime`, `game`, `gameid`, `userid`, `sum`, `username`, `numbers`, `count`, `how_much_win`, `win`, `checked`) VALUES
(1, '2014-10-22 16:33:18', '1', 6592, 40294, 20, 'sergey', '13|19|31|49|50|61|65', 0, 0, 'no', 'no'),
(2, '2014-10-22 16:33:20', '1', 6592, 40294, 20, 'sergey', '8|10|36|38|44|50|68', 0, 0, 'no', 'no'),
(3, '2014-10-22 16:33:22', '1', 6592, 40294, 20, 'sergey', '2|14|31|42|48|56|64', 0, 0, 'no', 'no'),
(4, '2014-10-22 16:33:23', '1', 6592, 40294, 20, 'sergey', '8|11|26|34|37|42|44', 0, 0, 'no', 'no'),
(5, '2014-10-22 16:33:24', '1', 6592, 40294, 20, 'sergey', '5|27|28|55|60|62|67', 0, 0, 'no', 'no'),
(6, '2014-10-22 16:33:27', '2', 6592, 40294, 160, 'sergey', '1|15|19|25|38|47|62|64', 0, 0, 'no', 'no'),
(7, '2014-10-22 16:33:28', '2', 6592, 40294, 160, 'sergey', '2|6|40|45|54|56|69|70', 0, 0, 'no', 'no'),
(8, '2014-10-22 16:33:30', '3', 6592, 40294, 720, 'sergey', '1|7|23|47|54|55|57|59|68', 0, 0, 'no', 'no'),
(9, '2014-10-22 16:33:36', '3', 6592, 40294, 1080, 'sergey', '3|12|15|26|33|41|43|46|60', 0, 0, 'no', 'no'),
(10, '2014-10-22 16:33:45', '1', 6592, 40294, 30, 'sergey', '17|26|31|55|57|59|61', 0, 0, 'no', 'no');

page

$arr = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT numbers FROM tickets WHERE checked = 'no'") or sqlerr(__FILE__, __LINE__);
    if (mysqli_num_rows($arr) > 0){
        while ($res = mysqli_fetch_assoc($arr)) {
               foreach (explode("|", $res['numbers']) as $value) {
                        $numbers[$value]++;
               }    
       }

$numbers_f = array();
foreach($numbers as $num => $value){
    $numbers_f[$num] += $value;
}

foreach(range(1, 70) as $num){
  $numbers_f[$num] = (isset($numbers_f[$num]) ? $numbers_f[$num] : 0);
}

rsort($numbers_f,SORT_NUMERIC); 
$top=array_slice($numbers_f,0,10); //top 10
$butt=array_slice($numbers_f,(53));  //bottom 17
print_r($top);  
//echo implode(",", $top);
echo"<br />";
//echo implode(",", $butt);
print_r($butt);
}

The results here is by $value, how to change the code to see the results by $num, and only sort by $value like now. thanks you very much for help.

  • 写回答

1条回答 默认 最新

  • doudang8824 2014-10-24 17:31
    关注

    According to your comment I think this is now the right solution for your problem.

    • Use arsort() instead of rsort() to preserve the key-value pairs.
    • Since PHP 5.0.2 array_slice() accepts the optional parameter preserve_keys, which should be set TRUE. Otherwise PHP reindexes the array.

          $input =  '2|10|8|10|22|44|5|38|3|22|10'; //input values for testing
          $numbers = array();
      
          foreach (explode("|", $input) as $value) {
              $numbers[$value]++;
          }    
      
          $numbers_f = array();
          foreach(range(1, 70) as $num){
            $numbers_f[$num] = (array_key_exists($num, $numbers_f) ? $numbers_f[$num] : 0);
          }   
      
      
          foreach($numbers as $num => $value){
              $numbers_f[$num] += $value;
          }    
      
          arsort($numbers_f); //preserving key-value pairs
      
          $top=array_slice($numbers_f, 0, 10, true); //top 10
          $butt=array_slice($numbers_f, 53, null, true);  //bottom 17
          print_r($top);  
          //echo implode(",", $top);
          echo"<br />";
          //echo implode(",", $butt);
          print_r($butt);
      

    Prior to PHP 5.0.2 a function like this could be used instead of array_slice():

    function slice( $array, $offset, $length = null ) {
        $keys = array_keys( $array ); // read the keys of the input array
        $values = array_values( $array ); // read the values of the input array
    
        $end = is_int( $length ) ? $length : ( sizeof( $array ) - $offset ); //length to slice
    
        $new = array();
    
        for ( $i = $offset; $i < $offset + $end; $i++ ) {
            $new[$keys[$i]] = $values[$i]; //fill new array with sliced content
        }
    
        return $new; //return sliced array
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。