PHP - Sorting Numbers Numerically
I have a txt file containing many numbers. I need to sort the values numerically from highest to lowest. When i use the rsort function, it sorts the numbers first by how many numbers long it is. E.g. 1,000 is sorted higher than 10,000. Any idea how I can fix this??
Similar TutorialsTrying to find an answer for this question he http://stackoverflow.com/questions/7547960/sorting-displaying-by-numeric-and-then-alphabetically If any of you guys can't help that would be great Hello, I was wondering how i could count how many times the number 1 appers in the document and 2, 3, 4, 5, 6, 7, 8, 9, 10. The text will have over 1000000 results set out like this: Code: [Select] 2 10 1 4 2 7 5 3 8 10 1 7 7 2 1 9 3 3 7 3 1 5 2 1 4 7 7 9 10 2 5 8 10 9 8 3 2 2 9 6 8 8 8 2 5 1 9 3 5 9 3 4 6 7 5 6 9 2 3 6 2 once it has counted the charicters i want it to put it in to a table I have all redey made the table: Code: [Select] <table border="1"> <tr> <th>1</th> <th>2</th> <th>3</th> <th>4</th> <th>5</th> <th>6</th> <th>7</th> <th>8</th> <th>9</th> <th>10</th> </tr> <tr> <td>number of numbers</td> <td>number of numbers</td> <td>number of numbers</td> <td>number of numbers</td> <td>number of numbers</td> <td>number of numbers</td> <td>number of numbers</td> <td>number of numbers</td> <td>number of numbers</td> <td>number of numbers</td> </tr> </table> I have looked at the count_chars function but the manule is ver confuzerling if anyone could explane how i could do this it would be great Thanks Jragon Hi, I need to sort variables in groups of up to 15 and put it in an array. For example: $exstract['center_tabOpBody_0'] =5 $exstract['center_tabOpBody_1'] =6 $exstract['center_tabOpBody_2'] =8 $exstract['center_tabOpBody_3'] =1 Should yield: ARRAY( = center_tabOpBody_1,center_tabOpBody_2,center_tabOpBody_3 // <-----15 [1] = center_tabOpBody_0 //<----5 ) Is there some simple function do do the "efficiency" sort? Thanks, Vadim Hello. I have a multi dimensional array that needs to be sorted. Array top25 [index][product id][number_of_purchases]. The index will contain a unique number for each product id. First product will be 1, second product will be 2, etc. The product id will be a id number like 34324ac9a89. The number of purchases will be how many times the item has been purchased. - End result: I want to print the 25 most purchased products in this format. product id - number of purchases a39acz - 503 a8cz9c - 480 zc8ac - 392 How do I do this? Thanks I have a situation where I need to sort files on a screen based on the date that is embedded into the file name. The problem is that I am sorting based off the file name, and when I get my output the LEGACY file always comes out on top of the sort because it starts with a 0 meanwhile the rest have dates appended the the filename so I don't have a problem sorting those. Please help me if you can. <? //Special case for extra files IF($Recordmyrow['App'] == "(Printable)") { $display = false; $column3 = "Printable VER"; IF($securityvalue == "P") { if(substr($path, 0, 5) == "file:") { $filename = substr($path, 5); if(file_exists($filename)) { $basename = substr($filename, 0, strrpos($filename, '.')); $ext = ".pdf"; $allfiles = glob("{$basename}*.pdf"); rsort($allfiles, SORT_NUMERIC); foreach($allfiles as $file) { if($ext == ".pdf") { $column4 = "<a href=\"pdf.php?src={$file}\">VIEW</a><br />\n"; $codate = substr($file, 70, 8); $co = substr($file, 59, 1); if ($codate != "pdf") { $column3 = "Printable File - ".$codate; } elseif ($codate == "pdf") { $column3 = "Printable File - LEGACY"; } IF($lastGroup != $Recordmyrow['Grouping']) { $lastGroup = $Recordmyrow['Grouping']; ?> <TR> <TD colspan="4" align="center" bgcolor="#418765"><B><FONT color="white"><? echo $Recordmyrow['Grouping'] ?></FONT></B></TD> </TR> <? } ?> <TR> <TD class="display" align="center" bgcolor="<?=changeColor($rowcount)?>"><B><? echo $column1 ?></B></TD> <TD class="display" bgcolor="<?=changeColor($rowcount)?>"><B><? echo $column2 ?></B></TD> <TD class="display" bgcolor="<?=changeColor($rowcount)?>"><B><? echo $column3 ?></B></TD> <TD class="display" bgcolor="<?=changeColor($rowcount)?>" align="center"><B><? echo $column4 ?></B></TD> </TR> <? $rowcount++; } } } else { $column4 = "Not Available"; ?> <TR> <TD class="display" align="center" bgcolor="<?=changeColor($rowcount)?>"><B><? echo $column1 ?></B></TD> <TD class="display" bgcolor="<?=changeColor($rowcount)?>"><B><? echo $column2 ?></B></TD> <TD class="display" bgcolor="<?=changeColor($rowcount)?>"><B><? echo $column3 ?></B></TD> <TD class="display" bgcolor="<?=changeColor($rowcount)?>" align="center"><B><? echo $column4 ?></B></TD> </TR> <? $rowcount++; } } } } Just a further question from a earlier post that was complete if i do a search and i want to order them from a drop box the if statement would it just look like Quote "select * from Table order by '".$_POST['order']."'" Or would this not work ? Thanx i need to sort my database results by the day they were added to the database. There is a column with the timestamp of when they were inserted but how do i group them by the day they were inserted so i can display them as such: Mon 14th: 5 records Tues 15th: 11 records etc. Part of my code: Code: [Select] <?php $arr = array ("Christina", "Daniel", "Andreea); reset($arr); foreach ($arr as $username) { //more code here ... foreach ($stats as $result) { echo $username."'s points - ".$result."<br />"; } } ?> And gives this: Code: [Select] Christina's points - 14 Daniel's points - 45 Andreea's points 23 My question is: can you sort the the $result so it will show Code: [Select] Daniel's points - 45 Andreea's points 23 Christina's points - 14 Thank you I have an array like this: $array[$row] = array ( 'name' = $name, 'discount' = $discount, 'price' = $price ); Now, I have about 200 of those in that array, and im trying to sort by the discount, highest 1st. How would I go about doing that? Thanks. First of all I am very new to php. I have coded in asp for years but have made the switch so I kinda know what I want to do but I am unsure on how to achieve it. I have a css menu that I am trying to populate through a database. The parent tabs are in a standard list. The first child submenu is in sepearate unordered list and anymore child tabs are within that list. Like this. Code: [Select] <div id="ddtopmenubar" style="width: 960px"> <ul> <li><a href="?page=">HOME </a></li> <li><a href="?page=About" rel="">ABOUT </a></li> <li><a href="?page=Services" rel="SERVICES">SERVICES </a></li> <li><a href="?page=Governance" rel="GOVERNANCE">GOVERNANCE </a></li> <li><a href="?page=Events">EVENTS </a></li> <li><a href="?page=Contact">CONTACT </a></li> </ul> <ul id="SERVICES" class="ddsubmenustyle"> <li><a href="?page=Health">Health/Dental</a></li> <li><a href="?page=Identification">International Student Identification Card</a></li> <li><a href="?page=Employment">Student Employment</a></li> <li><a href="?page=Saver">Student Saver</a></li> <li><a href="#">Other Services</a> <ul> <li><a href="#">Emergency Student Loan Fund</a></li> <li><a href="#">Legal Services</a></li> <li><a href="#">Parking Permits</a></li> <li><a href="#">Homes4Students</a></li> </ul> </li> </ul> <ul id="GOVERNANCE" class="ddsubmenustyle"> <li><a href="?page=Council">Your Council</a></li> <li><a href="?page=Executive">Executive</a></li> <li><a href="?page=Committees">Committees</a></li> <li><a href="?page=Constitution">Constitution/By-Laws</a></li> <li><a href="?page=Minutes">Minutes</a></li> <li><a href="?page=Elections">Elections</a></li> </ul> </div> I am trying to populate this from a database list the results come ordered like this Code: [Select] 1.0 2.0 3.0 3.1 3.2 3.3 3.4 3.4-1 3.4-2 3.4-3 3.4-4 4.0 4.1 4.2 4.3 4.4 4.5 4.6 5.0 6.0 where the "." show the 1st childs and the "-" show the 2nd child I tried to do this by splting each line and determining if it is a zero or not if it was a zero i put that paticular one into an array to be used in the sub menu however this did not work out. Any ideas? Hi does anyone know how to sort the array below (files) echo "<br /><br /><b>Favorites</b><br />"; $d = dir("/mnt/disk/v1/mfiles/downloads/music/favorites/"); while (false !== ($entry = $d->read())) { if($entry!='.' && $entry!='..') { if(is_dir($entry)) { echo 'true'; $subdirs = get_leaf_dirs($entry); if ($subdirs) $array = asort(array_merge($array, $subdirs)); else $array[] = $entry; } echo '<a href="/scripts/download.php?actie=favorites/'.$entry.'" title="Click here to download this song" target="_blank">'.left($entry,(strlen($entry)-4)).'</a><br />'; } } $d->close(); Is it possible to sort a table of mysql data, by using a variable? I'm trying this: Code: [Select] <?php $result = mysql_query("SELECT * FROM members ORDER BY '$totalpower'"); echo "<table border='1' cellpadding='10'>"; echo "<tr> <th>Rank</th> <th>Username</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo '<td> </td>'; echo '<td>' . $row['Username'] . '</td>'; echo "</tr>"; } echo "</table>"; ?> It display's a list of username's but not in the correct power order. $totalpower is defined: Code: [Select] <?php //power calculations $result = mysql_query("SELECT * FROM members WHERE Username='$_SESSION[Username]'") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { $power1 = $row['ounit1'] * 50; $power2 = $row['ounit2'] * 70; $power3 = $row['ounit3'] * 110; $power4 = $row['ounit4'] * 200; $power5 = $row['ounit5'] * 150; $power6 = $row['ounit6'] * 300; $power7 = $row['ounit7'] * 500; $power8 = $row['ounit8'] * 450; $power9 = $row['ounit9'] * 650; $power10 = $row['ounit10'] * 350; $power11 = $row['ounit11'] * 600; $power12 = $row['ounit12'] * 1000; $dpower1 = $row['dunit1'] * 50; $dpower2 = $row['dunit2'] * 70; $dpower3 = $row['dunit3'] * 110; $dpower4 = $row['dunit4'] * 200; $dpower5 = $row['dunit5'] * 150; $dpower6 = $row['dunit6'] * 300; $dpower7 = $row['dunit7'] * 500; $dpower8 = $row['dunit8'] * 450; $dpower9 = $row['dunit9'] * 650; $dpower10 = $row['dunit10'] * 350; $dpower11 = $row['dunit11'] * 600; $dpower12 = $row['dunit12'] * 1000; $dbase = $row['baselevel'] * 3000; } $offencepower = $power1+$power2+$power3+$power4+$power5+$power6+$power7+$power8+$power9+$power10+$power11+$power12; $defencepower = $dpower1+$dpower2+$dpower3+$dpower4+$dpower5+$dpower6+$dpower7+$dpower8+$dpower9+$dpower10+$dpower11+$dpower12+$dbase; $totalpower = (($offencepower+$defencepower)/ 100 * 30) + $offencepower+$defencepower; ?> Also how do I add auto numbers in the left column e.g 1. 2. 3. 4. Thanks in advance. I am using the following code to sort by Year. It displays the current year fine and current year also displays in selection but does give the option for the previous years selection. Code: [Select] <form action="archivednews.php" method="post"> <select name="year" id="year"> <?PHP for($i=date("Y"); $i<=date("Y"); $i++) if($year == $i) echo "<option value='$i' selected>$i</option>"; else echo "<option value='$i'>$i</option>"; ?> </select> <input type="submit" value="GO"> </form> This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=317320.0 i have an array from a database that looks like this (below) but what i want is for the array to be sorted assedningly by the start position so the team with start position 1 will be 1st and so on...if anyone can help me on what i should do please...thank you Code: [Select] Array ( [1] => Array ( [team_name] => A Team [team_nationality] => British [start_position] => 2 ) [2] => Array ( [team_name] => B Team [team_nationality] => British [start_position] => 1 ) [3] => Array ( [team_name] => C Team [team_nationality] => British [start_position] => 4 ) [4] => Array ( [team_name] => B Team [team_nationality] => British [start_position] => 3 ) ) I need to sort this: Code: [Select] customVar($var1, $var2, $var3); I have tried: Code: [Select] sort(customVar($var1, $var2, $var3)); I have an array that looks like this... Code: [Select] Array ( [0] => Array ( [0] => 2 [1] => Dylan Cross ) [1] => Array ( [0] => 5 [1] => Bob Smith ) [2] => Array ( [0] => 1 [1] => Sarah Park ) [3] => Array ( [0] => 7 [1] => Jane Knowles ) [4] => Array ( [0] => 6 [1] => Harry Hill ) [5] => Array ( [0] => 3 [1] => Jack Rock ) ) How do I sort this array so it is ordered alphabetically by the names so the result will be... Code: [Select] Array ( [0] => Array ( [0] => 5 [1] => Bob Smith ) [1] => Array ( [0] => 2 [1] => Dylan Cross ) [2] => Array ( [0] => 6 [1] => Harry Hill ) [3] => Array ( [0] => 3 [1] => Jack Rock ) [4] => Array ( [0] => 7 [1] => Jane Knowles ) [5] => Array ( [0] => 1 [1] => Sarah Park ) ) Thanks Hello, Hope somebody can help me. I have an XML file that is generated by active directory that dumps everything required for a telephone directory. I currently present this using XSL but would like to move towards PHP. I have some rudimentary code and it's working fine but I'd like to be able to sort by various values, such as First Name or telephone number. My XML format looks like this: Code: [Select] <?xml version="1.0" encoding="UTF-8"?> <telephonelist> <employee> <firstname>Jimmy</firstname> <lastname>Jimmerson</lastname> <email>jimbo@companyxyz.com</email> <extensionnum>956</extensionnum> <jobtitle>Security Officer</jobtitle> <department>Security</department> </employee> <employee> <firstname>David</firstname> <lastname>Davidson</lastname> <email>David.Davidson@companyxyz.com</email> <extensionnum>968</extensionnum> <jobtitle>Manager</jobtitle> <department>Management</department> </employee> Any my current code looks like this: Code: [Select] <? $myStaffMembers = new SimpleXMLElement("includes/telephonelist.xml", null, true); echo <<<EOF <table width="95%" border="0"> <tr> <th><b>First Name</b></th> <th><b>Last Name</b></th> <th><b>Telephone Number</b></th> <th><b>Position</b></th> </tr> EOF; foreach($myStaffMembers as $myStaffMember) { echo " <tr> <td>{$myStaffMember->firstname}</td> <td>{$myStaffMember->lastname}</td> <td>{$myStaffMember->extensionnum}</td> <td>{$myStaffMember->jobtitle}</td> </tr>"; } ?> Appreciate any help anybody could give. Thanks, LJ Hi - I'm modifying some PHP code that someone else wrote. Below is a snippet of code that basically takes mp3 files files within all the folders in a directory, and creates an RSS feed. In the resulting feed, the files are intermingled in date order. Instead, I'd like them sorted by the directory that they are in. So, if there are files in these directories: 01-First 02-Second 03-Third I'd like all the files in the "01-First" directory to come before the "02-Second" directory and so on. Within each directory the files can be sorted as they currently are. The name of the directory in the code is signified by $dir_name. I tried sort() and array_multisort() but I couldn't get either of them to work the way I wanted. Any thoughts? if ($url_params['mode'] == 'rss2') { print getRSSHeader('UTF-8', $device, $vm_config, $vm_name, $web_master, $logo_image,$context); $mailboxes = array(); if ($dev_dir = @opendir($VMHome . $device)) { //might fail if mailbox has not received first message yet while ($f = readdir($dev_dir)) { if (!eregi("tmp","$f") && !eregi("\.+","$f") && !eregi("Exclude-From-CD", "$f")) { //skip tmp directory and Exclude-From-CD Directories array_push($mailboxes,"$f"); #collect all existing mailboxes } } array_multisort($mailboxes, SORT_DESC); foreach ($mailboxes as $mailbox) { $files=array(); $dir_name = $VMHome . $device . '/'. $mailbox .'/'; $dir = opendir($dir_name); while ($f = readdir($dir)) { if (eregi("\.txt",$f)){ #if filename matches .txt in the name array_push($files,"$f"); #push into $files array } } foreach($files as $file){ $props = getProperties($dir_name, $file); $uid = getMP3File($dir_name, $file, $props, $device, $mailbox, $vm_config, $vm_name,$context); print getRSSItem ($props, $uid, $dir_name, $device, '/' . $mailbox . '/', $context); } } } print ' </channel> </rss>'; } Here is the code: Code: [Select] <table class="sortable"> <?php while($row = mysql_fetch_array($result)) { $title = $row["Rim"]; $model = $row["model"]; $dimenzija = $row["Dimenzija"]; $ime = $row['name']; $cena = $row['price']; $cena = number_format($cena, 2, ',', '.'); $id = $row['product_id']; if($i == 0) echo "<tr>"; echo "<td>" ?><a href="index.php?route=product/product&product_id=<?php echo $id?>"><?php echo $model ?></a> <?php echo "<br>$dimenzija<br>$ime<br>"; ?> <span style="color: #900; font-weight: bold;"><?php echo $cena . "din"?> </span> <?php echo "</td>"; if(++$i == $max_columns) { echo "</tr>"; $i=0; } }} if($i > 0) { for($j=$i; $j<$max_columns;$j++) echo "<td> </td>"; echo "</tr>"; } $prev = $start - $per_page; $next = $start + $per_page; if (!($start<=0)){ echo "<a href='{$_SERVER['PHP_SELF']}&start=$prev'>Prethodna strana </a>"; } $page = 1; for($x=0;$x<$record_count;$x=$x+$per_page){ if($start != $x){ echo " <a href='{$_SERVER['PHP_SELF']}&start=$x'> $page </a> ";} else{ echo " <a href='{$_SERVER['PHP_SELF']}&start=$x'> <b>$page</b> </a> ";} $page++; } if (!($start>=$record_count - $per_page)){ echo "<a href='{$_SERVER['PHP_SELF']}?route=product/search&start=$next'> Sledeća strana</a>"; } ?> </table> How can i create sorting button? Is it possible to do it without javascript (I am new to PHP and JS is still unknown to me)? |