php中数组排序代码的小改动

php中数组排序代码的小改动,php,arrays,sorting,numbers,Php,Arrays,Sorting,Numbers,首先我展示代码,以寻求帮助,最后我将解释我需要什么。 数据库 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', `game

首先我展示代码,以寻求帮助,最后我将解释我需要什么。 数据库

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');
$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);
}
页面

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');
$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);
}
$arr=mysqli\u query($GLOBALS[“\uuuuuuuu mysqli\u ston”],“从选中的票证中选择数字='no'”)或sqlerr(\uuuuuu文件,\uuuuuuuuu行);
如果(mysqli_num_行($arr)>0){
而($res=mysqli\u fetch\u assoc($arr)){
foreach(将(“|“,$res['numbers'])分解为$value){
$numbers[$value]++;
}    
}
$numbers_f=array();
foreach($num=>$value形式的数字){
$numbers\u f[$num]+=$value;
}
foreach(范围(1,70)为$num){
$numbers\u f[$num]=(isset($numbers\u f[$num])?$numbers\u f[$num]:0);
}
rsort($number\u f,SORT\u NUMERIC);
$top=array_slice($numbers_f,0,10);//前10名
$butt=数组_切片($numbers_f,(53));//底部17
打印(顶部);
//回波内爆(“,”,$top);
回声“
”; //回波内爆(“,”,$butt); 打印(butt); }

这里的结果是按$value,如何更改代码以按$num查看结果,并且只按$value排序,就像现在一样。非常感谢您的帮助。

根据您的评论,我认为这是解决您问题的正确方法

  • 使用而不是保留键值对
  • 因为PHP5.0.2接受可选参数preserve_keys,该参数应设置为TRUE。否则,PHP将重新索引数组

        $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);
    
    input='2 | 10 | 8 | 10 | 22 | 44 | 5 | 38 | 3 | 22 | 10'//用于测试的输入值
    $numbers=array();
    foreach(分解(“|”,$input)为$value){
    $numbers[$value]++;
    }    
    $numbers_f=array();
    foreach(范围(1,70)为$num){
    $numbers\u f[$num]=(数组\u键存在($num,$numbers\u f)?$numbers\u f[$num]:0);
    }   
    foreach($num=>$value形式的数字){
    $numbers\u f[$num]+=$value;
    }    
    阿索特(数字)//保留键值对
    $top=array\u slice($numbers\u f,0,10,true)//前十名
    $butt=数组\片($number\ f,53,null,true)//倒数第17名
    打印(顶部);
    //回波内爆(“,”,$top);
    回声“
    ”; //回波内爆(“,”,$butt); 打印(butt);
PHP5.0.2之前,可以使用类似这样的函数来代替array\u 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
}
函数片($array,$offset,$length=null){
$keys=array_keys($array);//读取输入数组的键
$values=array_values($array);//读取输入数组的值
$end=is_int($length)$length:$sizeof($array)-$offset);//要切片的长度
$new=array();
对于($i=$offset;$i<$offset+$end;$i++){
$new[$keys[$i]]=$values[$i];//用切片内容填充新数组
}
return$new;//返回切片数组
}

Mmm不,我不需要数组密钥。查看数据库(像17 | 26 | 31 | 55 | 57 | 59 | 61这样的数字),我的代码确实会检查有多少数字被更改,就像系统在两张票据中看到数字5一样,它会像5=2一样,所以现在我的数组将只打印计数(2=$value),但这很好,因为排序可以工作,但是如何更改或添加什么以按$num查看结果,我不想知道车票上有多少号码,我只想知道车票上的头号号码。)但是谢谢你的帮助,我现在可以结束这个问题了吗?