Jump to content

How to Sort Array values in Combo box.


theITvideos

Recommended Posts

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!! :)

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/211880-how-to-sort-array-values-in-combo-box/
Share on other sites

Thanks for the reply.

 

I have defined the array correctly in the right order but only in the combo it is all mixed up.

 

It is...  10 kg 11kg 1kg 2 kg

 

It should be 1kg 2kg ..... 10kg 11kg

 

What function should I use to sort the array in combo.

 

Thank you! :)

Don't sort it.  This produces exactly what you want:

 

$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' 
    );

echo "<select name=\"weight\">\n";
foreach($WeightArray as $option) {
echo "<option>$option</option>\n";
}
echo "</select>\n";

 

<select name="weight">
<option></option>
<option>100g or Less</option>
<option>101g to 250g</option>
<option>251g to 500g</option>
<option>501g to 1kg</option>
<option>1kg to 2kg</option>
<option>2kg to 3kg</option>
<option>3kg to 4kg</option>
<option>4kg to 5kg</option>
<option>5kg to 6kg</option>
<option>6kg to 7kg</option>
<option>7kg to 8kg</option>
<option>8kg to 9kg</option>
<option>9kg to 10kg</option>
<option>10kg to 11kg</option>
<option>11kg to 12kg</option>
</select>

This may not be the optimal solution, but as a workaround you can change the key values to all use the same unit of measurement, pad them with zeros then use ksort, such as this:

 

<?php
$WeightArray = array('' => '',
   '00100gOrLess' => '100g or Less',
   '00101g-00250g' => '101g to 250g',
   '00251g-00500g' => '251g to 500g',
   '00501g-01000g' => '501g to 1kg',
   '01001g-02000g' => '1kg to 2kg',
   '02001g-03000g' => '2kg to 3kg',
   '03001g-04000g' => '3kg to 4kg',
   '04001g-05000g' => '4kg to 5kg',
   '05001g-06000g' => '5kg to 6kg',
   '06001g-07000g' => '6kg to 7kg',
   '07001g-08000g' => '7kg to 8kg',
   '08001g-09000g' => '8kg to 9kg',
   '09001g-10000g' => '9kg to 10kg',
   '10001g-11000g' => '10kg to 11kg',
   '11001g-12000g' => '11kg to 12kg'
   );
ksort($WeightArray);
echo '<pre>'; print_r($WeightArray); echo '</pre>';
/**************
Returns:
Array
(
    [] => 
    [00100gOrLess] => 100g or Less
    [00101g-00250g] => 101g to 250g
    [00251g-00500g] => 251g to 500g
    [00501g-01000g] => 501g to 1kg
    [01001g-02000g] => 1kg to 2kg
    [02001g-03000g] => 2kg to 3kg
    [03001g-04000g] => 3kg to 4kg
    [04001g-05000g] => 4kg to 5kg
    [05001g-06000g] => 5kg to 6kg
    [06001g-07000g] => 6kg to 7kg
    [07001g-08000g] => 7kg to 8kg
    [08001g-09000g] => 8kg to 9kg
    [09001g-10000g] => 9kg to 10kg
    [10001g-11000g] => 10kg to 11kg
    [11001g-12000g] => 11kg to 12kg
)
*/
?>

 

EDIT: Or you can do like AbraCadaver said above, since he apparently didn't over-think this like I did . . .  :-\

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.