首先我展示代码,以获得帮助,最后我将解释我需要什么。数据库
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);
}
这里的结果是按$value,如何更改代码以查看按$num的结果,并且像现在一样只按>$value序。非常感谢你的帮助。
根据您的评论,我认为这是解决您问题的正确方法。
- 使用arsort()而不是rsort()来保留键值对
-
由于 PHP 5.0.2array_slice()接受可选参数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);
在 PHP 5.0.2之前,可以使用这样的函数来代替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
}