little change array sort code in php -



little change array sort code in php -

first show code, help me, , in end explain 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 `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 tickets checked = 'no'") or sqlerr(__file__, __line__); if (mysqli_num_rows($arr) > 0){ while ($res = mysqli_fetch_assoc($arr)) { foreach (explode("|", $res['numbers']) $value) { $numbers[$value]++; } } $numbers_f = array(); foreach($numbers $num => $value){ $numbers_f[$num] += $value; } foreach(range(1, 70) $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 $value, how alter code to see results $num, , sort $value now. much help.

according comment think right solution problem.

use arsort() instead of rsort() preserve key-value pairs.

since php 5.0.2 array_slice() accepts optional parameter preserve_keys, should set true. otherwise php reindexes array.

$input = '2|10|8|10|22|44|5|38|3|22|10'; //input values testing $numbers = array(); foreach (explode("|", $input) $value) { $numbers[$value]++; } $numbers_f = array(); foreach(range(1, 70) $num){ $numbers_f[$num] = (array_key_exists($num, $numbers_f) ? $numbers_f[$num] : 0); } foreach($numbers $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 function used instead of array_slice():

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

php arrays sorting numbers

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -