PHP - Sorting List/array
Hello. I am just trying to sort an array alphabetically. Here's the code I'm using:
function GetAssocCharities($registrationNo) { $db = ConnectionString::ConnectTo_www_ago_mo_gov_data(); $sqlstring = "SELECT * FROM charityPartners where RegistrationNo LIKE '$registrationNo' ORDER BY Name"; $results = @mysql_query($sqlstring); $org = null; while ($resultArray = mysql_fetch_array($results)) { $temp = new Registrants($resultArray['ARegNoFormatted'], $resultArray); $org[] = $temp; } return asort($org); } When I try to loop through the array using a foreach loop, I get an error that Invalid argument supplied for foreach(). Here's my foreach loop: $lstCharities = OrgQueries::GetAssocCharities($registrant->RegNoFormatted); foreach($lstCharities as $c) { //dom something here } I'm sure its something with not understanding list v array or something or that nature, but I'm not having much luck figuring it out with google. Thanks! Similar TutorialsHi!
I have this problem that I have to solve in PHP. I have such a difficult with this programming and I would be so grateful if someone could help me solve this.
The problem:
A lecturer has a group of students who need to be assigned to one of two groups for the I'm trying to sort this dropdown box. It reads from a directory, and lists the file name in the dropdown box. Here's the tricky part... the filename is listed differently in the dropdown than in the directory by using explode(). I want to sort it though since it's still being sorted by the directory listings... For example: Filename starts out as: 123_abc_567.pdf then gets listed as abc_123_567.pdf in the dropdown, but it's still getting sorted as if it were 123_abc_567.pdf How can I do that? Here's my code: // Define the full path to folder from root $path = "C:/Work_Orders/"; // Open the folder $dir_handle = @opendir($path) or die("Unable to open $path"); echo "<form method=\"POST\" action='".$_SERVER['PHP_SELF']."' name='selectworkorder'><select name='ordernumber2'>"; // Loop through the files while ($file = readdir($dir_handle)) { //Remove file extension $ext = strrchr($file, '.'); if($ext !== false) { $file = substr($file, 0, -strlen($ext)); } if($file == "." || $file == ".." || $file == "index.php" ) continue; //explode file name $changedordernumber = explode("_",$file); //put in new order $changedordernumber = $changedordernumber[1]."_".$changedordernumber[0]."_".$changedordernumber[2]; $changedordernumber=trim($changedordernumber,"_"); //list options echo "<option name='$file' value='$file'>$changedordernumber</option>\n"; } echo "</select><input type='submit' value='Change' name='submit'/></form></div>"; // Close closedir($dir_handle); Hi first tyvm for reading my help request me and my friend ar trying to build a order form (see png file ^^) so the main idea is some on go to order form.php
in that order form they fil in thear info like name, adress you know ^^
thean here is the treaky part at the and of the form they kan add products to there order by selecting it or search it from the drop down list the items in the dropdown list come from msql
the client can add more or delete items by clicking - or + whean the clint click send the client need to get a mail whit there a list of what theay have orderd and there unic order number wel so far we can do that but we wont to send another list to the company of order where the added products in the order ar sorted out based on the product id number
this is what we have so far http://clientconstru...sion.nl/valeri/
tyvm again voor anyone who read this ^^
Attached Files
bestel-form.png 37.78KB
0 downloads I am attempting to sort an array by price but cannot seem to get php to do this. Below is the array I am attempting to sort by the value ~ price: Array ( [http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(550992137)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165488_productId_561801_langId_-1%3Fsource%3Dtd)] => Array ( [logo] => nc0iicjxmq.gif [stock] => In Stock In Stock [price] => 14.99 [delivery] => 4.99 [url] => http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(550992137)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165488_productId_561801_langId_-1%3Fsource%3Dtd) [mname] => Halfords ) [http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(767538675)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165497_productId_780417_langId_-1%3Fsource%3Dtd)] => Array ( [logo] => nc0iicjxmq.gif [stock] => In Stock In Stock [price] => 19.99 [delivery] => 4.99 [url] => http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(767538675)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165497_productId_780417_langId_-1%3Fsource%3Dtd) [mname] => Halfords ) [http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(254177017)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165497_productId_271987_langId_-1%3Fsource%3Dtd)] => Array ( [logo] => nc0iicjxmq.gif [stock] => In Stock In Stock [price] => 15.00 [delivery] => 2.99 [url] => http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(254177017)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165497_productId_271987_langId_-1%3Fsource%3Dtd) [mname] => Halfords ) [http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(550992487)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165488_productId_561805_langId_-1%3Fsource%3Dtd)] => Array ( [logo] => nc0iicjxmq.gif [stock] => In Stock In Stock [price] => 4.99 [delivery] => 2.99 [url] => http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(550992487)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165488_productId_561805_langId_-1%3Fsource%3Dtd) [mname] => Halfords ) [http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(18823539)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165488_productId_155878_langId_-1%3Fsource%3Dtd)] => Array ( [logo] => nc0iicjxmq.gif [stock] => In Stock In Stock [price] => 18.00 [delivery] => 4.99 [url] => http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(18823539)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165488_productId_155878_langId_-1%3Fsource%3Dtd) [mname] => Halfords ) [http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(580777789)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_242553_productId_385055_langId_-1%3Fsource%3Dtd)] => Array ( [logo] => nc0iicjxmq.gif [stock] => In Stock In Stock [price] => 21.99 [delivery] => Free Shipping [url] => http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(580777789)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_242553_productId_385055_langId_-1%3Fsource%3Dtd) [mname] => Halfords ) [http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(18823503)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165497_productId_217063_langId_-1%3Fsource%3Dtd)] => Array ( [logo] => nc0iicjxmq.gif [stock] => In Stock In Stock [price] => 24.99 [delivery] => Reserve and Collect [url] => http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(18823503)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165497_productId_217063_langId_-1%3Fsource%3Dtd) [mname] => Halfords ) [http://track.webgains.com/click.html?wgcampaignid=72782&wgprogramid=2730&product=1&wglinkid=90005&productname=Continental+Sport+Contact+Road%2FHybrid+Bicycle+Tyre&wgtarget=http://www.awcycles.co.uk/brands/Continental/Sport_Contact_Road/Hybrid_Bicycle_Tyre] => Array ( [logo] => 8yfd588e8t.jpg [stock] => In Stock In Stock [price] => 21.99 [delivery] => [url] => http://track.webgains.com/click.html?wgcampaignid=72782&wgprogramid=2730&product=1&wglinkid=90005&productname=Continental+Sport+Contact+Road%2FHybrid+Bicycle+Tyre&wgtarget=http://www.awcycles.co.uk/brands/Continental/Sport_Contact_Road/Hybrid_Bicycle_Tyre [mname] => AW Cycles ) ) Any help would be much appreciated. Thanks Simon 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(); 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 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>'; } 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 have the following php array structure and couldnot able to sort it according to the overall points. My code is reading an xml file using simplexml. Any help would be appreciated. Array ( => SimpleXMLElement Object ( [played] => 0 [won] => 0 [drawn] => 0 [lost] => 0 [overallpoints] => 0 ) [1] => SimpleXMLElement Object ( [played] => 6 [won] => 3 [drawn] => 0 [lost] => 3 [overallpoints] => 9 ) [2] => SimpleXMLElement Object ( [played] => 3 [won] => 1 [drawn] => 0 [lost] => 2 [overallpoints] => 3 ) [3] => SimpleXMLElement Object ( [played] => 0 [won] => 0 [drawn] => 0 [lost] => 0 [overallpoints] => 0 ) [4] => SimpleXMLElement Object ( [played] => 2 [won] => 1 [drawn] => 0 [lost] => 1 [overallpoints] => 3 ) } The following code works, but I need to take it a step further and I'm not sure how. What I'd like to do is sort the output so the highest point totals get printed first (along with the associated information). Can anyone help? Here's the site: http://www.beat-the-spread.net/seasontotals.php Here's the code: Code: [Select] $i=1; $w=1; $result = mysql_query( " SELECT team_name, owner FROM teams" ); while ($row = mysql_fetch_array($result)) { echo "<tr>\n", "</td><td>", $row['owner'], "</td><td>", $row['team_name']; $result1 = mysql_query( " SELECT bye, owner_pts FROM schedule WHERE team = $i"); $num_rows = mysql_num_rows($result1); while ($row1 = mysql_fetch_array($result1)) { $totalpts = $totalpts + $row1['owner_pts']; if (empty($row1['bye'])) { echo "</td><td>", $row1['owner_pts'], "</tr>\n"; } else { echo "</td><td>","0","</tr>\n"; } } while ($num_rows<=16) { echo "</td><td>","","</tr>"; $num_rows++; } echo "</td><td>","$totalpts","</tr>"; $totalpts=0; $w++; $i++; } echo "</table>\n"; mysql_close($con); Thanks! Hello, i need some help with sorting arrays by it's value. lets say i want to have two different html tables listing the employees and another for the managers, then i want to sort them by Group ID's: Etc: Workers as the key / 1 as the value. Groups: Workers => 1 Managers => 2 Workers Group ID John 1 Peter 1 Lance 1 Managers Group ID Carol 2 Greg 2 Brian 2 How can i do this? How to sort PHP2d Array using upon key. eg. I have a array of $users[0]['name'] = "abc" $users[0]['age'] = "12" $users[0]['sex'] = "M"; $users[1]['name'] = "xyz" $users[1]['age'] = "15" $users[1]['sex'] = "M"; $users[2]['name'] = "pqr" $users[2]['age'] = "16" $users[2]['sex'] = "F"; Now I have to sort in on age or 'sex', Hello! I have a 2D array that look as follows: $test["name"][0] = "arnold"; $test["name"][1] = "nate"; $test["name"][2] = "steve"; $test["id"][0] = 2; $test["id"][1] = 0; $test["id"][2] = 1; This means arnold has an ID of 2, nate has an ID of 0 and steve has an ID of 1. Now I want to sort the array based on the ID value, so I want to achieve the following result: $test["name"][0] = "nate"; $test["name"][1] = "steve"; $test["name"][2] = "arnold"; How would I achieve this? Help much appreciated. So i've got a CSV file, pull data from it and it's stored within an array. $data[0] contains date that looks like so 9/14/2011 00:00:00. How can i sort this array before displaying it so that it's sorted by Date ASC by default. Code: [Select] <?php if (($handle = fopen("http://www.website.com/images/uploads/Recruitment_for_Web.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) { echo '<tr>'; echo '<td>' . str_replace("0:00:00","",$data[0]) . '</td>'; echo '<td>' . $data[1] . '</td>'; echo '<td>' . $data[2] . '</td>'; echo '<td>' . $data[3] . '</td>'; echo '<td>' . $data[4] . '</td>'; echo '<td>'; if($data[7] != "") { echo '<a href="' . $data[7] . '" target="_blank">' . $data[5] . '</a>'; } else { echo $data[5]; } echo '</td>'; echo '<td>' . $data[6] . '</td>'; echo '</tr>'; } fclose($handle); } ?> Thanks Generally I am able to help my self on the forums. But, this one has me stumped. I've done it before and I don't remember it being this hard. The array looks something like so $users['id'] = '00001','00002','0003' $users['jobs'] = '22','322','112' $users['connected'] = '12','210','89' $users['percent'] = '54','65','89' I've used so much different things to try and take those, keep the data together (index 0 might be moved to 2, but all values from 0 are moved to 2) and sort by 'percent'. So I can display the person that has the highest percentage first. Basically the data was originally a CSV that is now in a database and is pulled, percentages calculated and then displayed. But the order is off. There is a bunch more data then show but I'm using PHP to do all the math for adding up jobs, seeing whats connected right and then coming up with a percentage, sticking it all in an array so I can sort it; but for some reason I cannot remember how I've done this before. Hi All I'm new to php and I have a multidimensional array set up which I need to sort for a results list from an assessment. The array is $gradout['name']['userid']=score. I need to sort the array by score in a decending order. ['name'] is a string and ['userid'] is a numeric. I've tried looking at the php manual but It's just confusing me. Can anyone explain to me in laymans terms what I need to do and why. I want to create an array based on data in a multidimensional array. Take, for example: $table[1] = array('husband' => array ('firstname'=> 'Albert', 'lastname' => 'Einstein', 'age' => 129), 'wife' => array ('firstname' => 'Mileva', 'lastname' => 'Einstein', 'age' => 128)); What would be the best approach, when you want to create an array with only the last names or the ages? Are there some things like wildcards in the PHP array universe to skip the first array level? Any suggestion is very appreciated. I tried several strategies, but ended up creating the same mulitdimensional array as above. I have an array and when i do a print_r(), i get this: Array ( => Array ( [path] => 1 [order] => 2 ) [1] => Array ( [path] => 2 [order] => 2 ) [2] => Array ( [path] => 3 [order] => 1 ) ) I want to sort it so that the array with an order of 1 is at the front, i.e. that last array in this case. I tried asort but that didnt do anything... I have an array of type Code: [Select] Array ( [2] => Array ( [name] => Shelby Baptist Medical Ctr [city] => Alabaster ) [1] => Array ( [name] => Andalusia Regional Hospital [city] => Andalusia ) [0] => Array ( [name] => Long Term Hospital of Anniston [city] => Anniston )) How can sort the keys of an array but there content remains the same so that they are like Code: [Select] Array ( [0] => Array ( [name] => Shelby Baptist Medical Ctr [city] => Alabaster ) [1] => Array ( [name] => Andalusia Regional Hospital [city] => Andalusia ) [2] => Array ( [name] => Long Term Hospital of Anniston [city] => Anniston )) Can someone please point me if there is any func. for this Hi, I have got some data out of my database, I would now like to sort all of this by putting the data into arrays, something looking like this: branch - costs - profit France - 406.85 - 1392.48 So each branch is one item in the array, but has the costs and profit with it too. I would then like to sort those arrays by things like branch name, costs and profit. Is this possible? |