PHP - How To Sort Array Values In Combo Box.
Hi there.
I am trying to sort values in a combo box stored in an array. The array is written in t his way. Here this is in proper sorting: $WeightArray = array('' => '', '100gOrLess' => '100g or Less', '101g-250g' => '101g to 250g', '251g-500g' => '251g to 500g', '501g-1kg' => '501g to 1kg', '1kg-2kg' => '1kg to 2kg', '2kg-3kg' => '2kg to 3kg', '3kg-4kg' => '3kg to 4kg', '4kg-5kg' => '4kg to 5kg', '5kg-6kg' => '5kg to 6kg', '6kg-7kg' => '6kg to 7kg', '7kg-8kg' => '7kg to 8kg', '8kg-9kg' => '8kg to 9kg', '9kg-10kg' => '9kg to 10kg', '10kg-11kg' => '10kg to 11kg', '11kg-12kg' => '11kg to 12kg' ); Now when I call this array and put in a combo it is displayed as shown in the Screenshot. It is displaying as, 10kg, 11kg 1kg ... in the incorrect order. I tried few sort() functions but I am not able to sort the array as shown in the screenshot attached. Kindly let me know how I can sort the values in the Combo box. Thank you!! Similar Tutorialshi, also i have an array with this values: $a=array('111-aa-zz-4','222-aa-zz-6','333-aa-zz-5'); and i want to sort(); this array, but it must ignore the numbers behind -aa-zz- also 4,6,5 how should i do this? best regards, tastro Hi there, I am working on a PHP web form and I have a combo box and I'm trying to sort the values in it. The values in the combo box are filled using an arraylist: $WeightArray = array('' => '', '0|100' => '100g or Less', '101|250' => '101g to 250g', '251|500' => '251g to 500g', '501|1000' => '501g to 1kg', '1000|2000' => '1kg to 2kg', '2000|3000' => '2kg to 3kg', '3000|4000' => '3kg to 4kg', '4000|5000' => '4kg to 5kg', '5000|6000' => '5kg to 6kg', '6000|7000' => '6kg to 7kg', '7000|8000' => '7kg to 8kg', '8000|9000' => '8kg to 9kg', '9000|10000' => '9kg to 10kg', 'customValues' => 'Custom Values' ); The values get properly filled in the combo box. Thats fine. Its just that the values are not sorted the way I want them to. The Grams and Kilograms are all mixed. It should output in the order defined in the array. See the ScreenShot I've taken and see the difference in the values defined and the output. I tried few sorting methods like ksort($WeightArray); But still the same output. Whats the good trick to sort values in Combo as per the array. All comments and feedback are always welcomed. Thank you. Hi there, I am having a PHP form with simple combo box. Here is the code: <form name="form1" method="get" action=""> <select name="select" onChange="form1.submit()"> <option value='value1'>My Value1</option> <option value='value2'>My Value2</option> <option value='value3'>My Value3</option> </select> </form> Now the form is successfully being submitted upon 'onChange' event. The only thing I am trying to do is to retain the value of the new option. As every time I select the value from the combo, it goes back and displays the 'value 1' even when I select value 3 or 2. How do I make the combo box retain the value as 3 (when I select option 3) after the form is submitted. Please reply. Thank you! how to connect juice and 1 and milk and 3 and cola and 2? and then sort it by the numbers from high to low? $a=array('juice','milk','cola'); $a2=array('1','3','2'); Hi guys, I'm trying to sort an array. The array has information about listings. ID, name, info etc. An unrelated set of data also contains the listing ID's and I have made a function to sort this array and show an new order for the ID's. I now need to sort the original array by this new ID order... This snippet shows that the original listings array can be sorted by link_id: sort($this->links, $this->link->link_id); //This works. It sorts links by link ID! In a foeach loop I have created this: $sleeps_array[] = array("ID" => $link->link_id, "Sleeps" => $sleeps); // Makes an array of arrays of a custom field with listing ID. It makes this: array ( 0 => array ( 'ID' => '9', 'Sleeps' => '2', ), 1 => array ( 'ID' => '3', 'Sleeps' => '4', ), 2 => array ( 'ID' => '6', 'Sleeps' => '6', ), ) I can then sort this array of arrays with this function: function compare_sleeps($a, $b){ return strnatcmp($a['Sleeps'], $b['Sleeps']); } # sort alphabetically by name usort($sleeps_array, 'compare_sleeps'); It then gives me a sorted array according to "Sleeps" (as in, the number of people a property can cater for). It has the correct order of ID to then output but how do I organise the original array by ID with this new array? Sounds complicated, I hope you got that! Cheers, RJP1 Why does krsort return the array as "1"? Code: [Select] $m = array('34' => 1122, '6' => 1944, '9' => 1710); print_r($m); //outputs: Array ( [34] => 1122 [6] => 1944 [9] => 1710 ) print_r(krsort($m)); //outputs: 1 What i need is the output to be Array ( [6] => 1944 [9] => 1710 [34] => 1122) Is there a way to use sort($Array) on numbers but reverse it. So instead of 1 to 3 itll do 3 to 1 ? AKA descending order? Can't find a function that would do unless theres a second sort function ? Thanks Hi, I have an array of type array_name[roll_number][aggregate_marks]. How do I sort such array on 'aggregate_marks'? My attempts on sort, asort haven't worked. Hi,
i have an issue to sort gallery photos by date. Right now its sort by Value (Name).
here is the code
index.php contains :
<div> <?php foreach($categories_array as $photo_category=>$photos_array){?> <?php $category_thumbnail = $gallery_url."/layout/pixel.gif"; if(file_exists('files/'.$photo_category.'/thumbnail.jpg')){ $category_thumbnail = $gallery_url.'/'.$photo_category.'/thumbnail.jpg'; } $category_url = $gallery_url.'/'.$photo_category; ?> <span class="category_thumbnail_span" style="width:<?php echo $settings_thumbnail_width;?>px; height:<?php echo $settings_thumbnail_height+20;?>px;"> <a class="category_thumbnail_image" href="<?php echo $category_url;?>" style="width:<?php echo $settings_thumbnail_width;?>px; height:<?php echo $settings_thumbnail_height;?>px; background-image:url('<?php echo $gallery_url;?>/layout/lens_48x48.png');" title="<?php echo htmlentities(ucwords(str_replace('-', ' ', $photo_category)));?>"> <img src="<?php echo $category_thumbnail;?>" width="<?php echo $settings_thumbnail_width;?>" height="<?php echo $settings_thumbnail_height;?>" alt="<?php echo htmlentities(ucwords(str_replace('-', ' ', $photo_category)));?>" /> </a> <a class="category_thumbnail_title" href="<?php echo $category_url;?>" title="<?php echo htmlentities(ucwords(str_replace('-', ' ', $photo_category)));?>"> <?php echo htmlentities(str_replace('-',' ', truncate_by_letters($photo_category, 16, '..')), ENT_QUOTES, "UTF-8");?> (<?php echo count($photos_array);?>) </a> </span> <?php } ?> </div>the sort function : ksort($categories_array); i need some help sorting my arrays. i have a class that selects data from database, and holds it inside a class to be used multiple times. rather then executing multiple queries. i know how to sort an array. but i am sorting arrays inside an array $data = arrray( array( 'id'=>1, '_name'=>'Server A', '_date_added'=>1298207645.5424, ); array( 'id'=>2, '_name'=>'Server B', '_date_added'=>1298247384.4839, ); array( 'id'=>3, '_name'=>'Server C', '_date_added'=>1298218493.0493, ); ); i want to sort my array descending by key _date_added hope you understand... Hi freaks, Got a simple one for ya, I THINK? I have a multi-array that resembles this.. Code: [Select] $_SESSION["book_array"] = array(0 => array("plantID" => $plantID, "botanicalName" => $botanicalName, "commonName" => $commonName, "use" => $use)); I would like to sort it by the botanicalName key of the inner array before I call the forech loop that renders the display so that after it renders the table the items will be seen alphabetically, code below.. Code: [Select] <?php $bookOutput = ""; //$plant_use_array = ''; if (!isset($_SESSION["book_array"]) || count($_SESSION["book_array"]) < 1) { $bookOutput = '<tr><td colspan="4"><h6>Your Book is EMPTY!</h6></td></tr>'; } else { // Start the For Each loop $i = 0; foreach ($_SESSION["book_array"] as $each_item) { $plantID = $each_item['plantID']; $botanicalName = $each_item['botanicalName']; $botanicalName = stripslashes($botanicalName); $commonName = $each_item['commonName']; $commonName = stripslashes($commonName); $use = $each_item['use']; //$x = $i + 1; // Dynamic table row assembly $bookOutput .= "<tr>"; $bookOutput .= '<td><a href="plant_details.php?plantID=' . $plantID . '" id="bodyLink">' . $botanicalName . '</a></td>'; $bookOutput .= '<td>' . $commonName . '</td>'; $bookOutput .= '<td><strong>' . $use . '</strong></td>'; $bookOutput .= '<td><form action="book.php" method="post"><input name="removeBtn" type="submit" value="Remove"/><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>'; $bookOutput .= '</tr>'; $i++; } } ?> Any help would be much appreciated! Thanks in advance. hi, i'm trying to sort an array of associative arrays, based on one of the keys. if the value of the key is a primitive, normal sorting occurs - this works fine. if the value of the key is an array, i'd like to have it sorted by "groups" here's an example: Code: [Select] <?php $data[] = array('name' => 'h', 'list' => array(1,2)); $data[] = array('name' => 'g', 'list' => array(1)); $data[] = array('name' => 'a', 'list' => array(1,3)); $data[] = array('name' => 'f', 'list' => array(2)); $data[] = array('name' => 'e', 'list' => array(2,3)); $data[] = array('name' => 'b', 'list' => array(3)); $data[] = array('name' => 'c', 'list' => array(1,2,3,4)); $data[] = array('name' => 'd', 'list' => array(3,4)); function deep_sort($array, $sorton){ usort($array, function($a, $b) use($sorton) { $a = $a[$sorton]; $b = $b[$sorton]; if(is_array($a) && is_array($b)){ // this bit is obviously flawed - only included for illustrative purposes $a = implode('', $a); $b = implode('', $b); } return ($a == $b) ? 0 : ($a > $b) ? 1 : -1; }); return $array; } $sorted_by_name = deep_sort($data, 'name'); print '<pre>'; print_r($sorted_by_name); print '</pre>'; $sorted_by_list = deep_sort($data, 'list'); print '<pre>'; print_r($sorted_by_list); print '</pre>'; ?> the sorted_by_name bit is fine - but for sorted_by_list, what i want returned is all the array ordered as follows: all those with "1" (or the lowest if none have "1") in it's list value first, then all those remaining that have the next highest ("2"), etc. so right now, the returns for sort_by_list look like this: Code: [Select] Array ( [0] => Array ( [name] => g [list] => Array ( [0] => 1 ) ) [1] => Array ( [name] => f [list] => Array ( [0] => 2 ) ) [2] => Array ( [name] => b [list] => Array ( [0] => 3 ) ) [3] => Array ( [name] => h [list] => Array ( [0] => 1 [1] => 2 ) ) [4] => Array ( [name] => a [list] => Array ( [0] => 1 [1] => 3 ) ) [5] => Array ( [name] => e [list] => Array ( [0] => 2 [1] => 3 ) ) [6] => Array ( [name] => d [list] => Array ( [0] => 3 [1] => 4 ) ) [7] => Array ( [name] => c [list] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) ) )whereas i'd like to get back: Code: [Select] Array ( [0] => Array ( [name] => g [list] => Array ( [0] => 1 ) ) [1] => Array ( [name] => h [list] => Array ( [0] => 1 [1] => 2 ) ) [2] => Array ( [name] => c [list] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) ) [3] => Array ( [name] => a [list] => Array ( [0] => 1 [1] => 3 ) ) [4] => Array ( [name] => f [list] => Array ( [0] => 2 ) ) [5] => Array ( [name] => e [list] => Array ( [0] => 2 [1] => 3 ) ) [6] => Array ( [name] => b [list] => Array ( [0] => 3 ) ) [7] => Array ( [name] => d [list] => Array ( [0] => 3 [1] => 4 ) ) ) TYIA for any suggestions I've got a multidimensional array that has 2 columns. The first column is numerical (country_id), and the second column is text (country_name), which may or may not be data that will be turned into an array. Looks something like this: column 1 (index 0) = 42 column 2 (index 1) = Great Britain|England I use a while loop to create an array of these rows, then a use a foreach loop to go through index[1] of each row. If there is a single name for a country, it is added to a new array, but if there is more than one name for a country, then both names are added to the new array. So, for example, if we had this data: row1 column 1 (index 0) = 12 column 2 (index 1) = Algeria row2 column 1 (index 0) = 22 column 2 (index 1) = Ethiopia row3 column 1 (index 0) =42 column 2 (index 1) = Great Britain|England Then the final array would look like this: index[0] = 12 index[1] = Algeria index[0] = 22 index[1] = Ethiopia index[0] = 42 index[1] = Great Britain index[0] = 42 index[1] = England Now I would like to sort by index[1] to get the following alphabetical order: index[0] = 12 index[1] = Algeria index[0] = 42 index[1] = England index[0] = 22 index[1] = Ethiopia index[0] = 42 index[1] = Great Britain What do I use? I've fooled around with sort, usort, asort ... can't figure it out. Thanks in advance. I have a bunch of PDF's, some of them having Uppercase first letters and some having lowercase first letters. Right now my code is alphabetizing all of the Uppercase first letters first then all of the lowercase. I want them to be mixed. $dir = "../../forms/pdf/"; foreach(glob($dir.'*.pdf') as $pdf){ $files[] = str_replace($dir, '', $pdf); } then: <select name="current_pdf" id="current_pdf"> <option value="">Please Select a PDF to replace</option> <?php foreach($files as $file) { echo "<option value=\"$file\">" . $file . "</option>"; } ?> </select> Hello, Im struggling with this, probably because of my lack of fundamental knowledge of arrays. What i want to do is: make an array with two keys. 'id' and 'distance'. I want to sort the results by distance, displaying the id's... im looking at ksort() as the solution, however i don't think im implementing it properly as i dont think i have set the array up properly!! what i have is not good... Code: [Select] <?php while ($row = mysql_fetch_array($result)) { //get the photographers postcode and split it into 2 pieces $p_postcode = explode(' ',$row['address_post_code']); //get the photographers postcode and split it into 2 pieces $u_postcode_a = explode(' ',$u_postcode); //run the two postcodes throught the function to determin the deistance $final_distance = return_distance($u_postcode_a[0], $p_postcode[0]); //start the variable so we can add to it later on $photographers_inrange = ""; //declare this variable to help with the comma spacing too EDIT LOOK TO LINE 46 //$i2 = 1; $photographers_inrange = array(); //if the distance is smaller or equal to the distance the photographer covers if($final_distance <= $row['cover_distance']){ //get their id $photographers_inrange['id'] = $row['ID'].','; $photographers_inrange['distance'] = ''.$final_distance; //echo $photographers_inrange; //EDIT: this method does not work when just one result is returned. now i use substr -1. //if this isnt the last result //if($i++ <= $i2++){ //then add a comma for the sql statement //$photographers_inrange .= ','; //} } ksort($photographers_inrange); foreach ($photographers_inrange as $key => $val) { echo "togs[" . $key . "] = " . $val . "<br/>"; } ?> with $photographers_inrange being where it is, i am getting alot of empty arrays. not what i want. i just need to know how to add to the original array, and how to then sort it. thanks for your ongoing help!! Hello all, I have a rather large array and I need to list it's contents in an alphabetical list. The array is built like this: $array['query']['row']['0']['title'] = 'Bob'; $array['query']['row']['1']['title'] = 'Andy'; $array['query']['row']['2']['title'] = 'Tom'; I need to sort it so it is in alphabetical order based on the title. Is there an easy way to do this? I've tried various sort methods on this array. And seem to be failing. Code: [Select] Array ( [monkey quest] => 8 [monkey] => 2 [monkey sports] => 0 [monkey go happy] => 0 [monkey go happy 3] => 1 [monkey joes] => 0 [monkey games] => 2 [monkey bread recipe] => 1 [monkey go happy 2] => 3 [monkey quest trailer] => 4 [monkey quest guide] => 5 [monkey quest nick] => 6 ) What I want to do or attempt to do rather is sort the array by the => value from highest to lowest (or maybe in reverse as well, but one thing at a time) My last failed attempt I was trying out usort() as my concept.. Code: [Select] function cmp($a, $b) { if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; } echo "<pre>"; print_r(usort($kw, "cmp")); echo "</pre>"; But the only thing I am left with on the page is just 1 everything else seems to be getting lost, or I dunno whats going on. So I think I've done stumped my self. Now I'm looking for idea's assuming I am tackling this all wrong. Hello, I'm making a imagegallery and when someone upload a new image that image has to appear as first picture of the gallery. I'm using an array for the files, but how can I sort the files on date, newest first.? <?php $n = 0; $pathToThumbs = "images/tekeningen/"; $dir = opendir( $pathToThumbs ); while (false !== ($fname = readdir($dir))){ // strip the . and .. entries out if ($fname != '.' && $fname != '..'){ $tekeningen[$n] = $fname; } echo $tekeningen[$n]."<br />"; $n = $n +1; } closedir( $dir ); ?> I found solutions with ksort and filemtime, but I don't understand them and/or they didn't work for me. I am running a script to retrieve numerical value from a webpage, the script echoes the numbers. However I need to add all the numbers to an array or txt file and then have them sorted from highest value to lowest value. Any hints or tips to how I would achieve this? Hi all, I have a multi-dimensional array looking somewhat like the following: Array ( [0] => Array ( [id] => 853 [title] => item 853 [price] => 17.99 [createdtime] => yyyy-mm-dd 00:00:00 ) [1] => Array ( [id] => 854 [title] => item 854 [price] => 11.99 [createdtime] => yyyy-mm-dd 00:00:00 ) ... I need to re-order this array so that any items with the 'createdtime' within a month from todays date are at the top, followed by all of the other items in the original order. I would usually amend the sql but I need to do this with the array. What would be the best way to do this? I found array_multisort but im not too sure how to adapt this to my needs. Thank you for any help with this! |