jason310771 Posted September 16, 2013 Share Posted September 16, 2013 For some reason I can not sort the array list, can anyone tell me what I am doing wrong ? My output is... Array ( [0] => 2 [1] => 5 [2] => 3 ) 1 $product_length = 2; $product_width = 5; $product_height = 3; // work out volume. $sizeArray = array(); $sizeArraySORTED = array(); $sizeArray = array($product_length, $product_width, $product_height); print_r($sizeArray); ?><br><br><br><? $sizeArraySORTED = asort($sizeArray, SORT_NUMERIC); print_r($sizeArraySORTED); Quote Link to comment https://forums.phpfreaks.com/topic/282183-unable-to-sort-array/ Share on other sites More sharing options...
Barand Posted September 16, 2013 Share Posted September 16, 2013 asort() does not return an array. It sorts the array in the parameter. Quote Link to comment https://forums.phpfreaks.com/topic/282183-unable-to-sort-array/#findComment-1449688 Share on other sites More sharing options...
vinny42 Posted September 16, 2013 Share Posted September 16, 2013 and it returns a boolean so print_r($sizeArraySORTED); cannot give you the output that you are claiming to get... Quote Link to comment https://forums.phpfreaks.com/topic/282183-unable-to-sort-array/#findComment-1449704 Share on other sites More sharing options...
cyberRobot Posted September 16, 2013 Share Posted September 16, 2013 and it returns a boolean so print_r($sizeArraySORTED); cannot give you the output that you are claiming to get... There are two print_r() statements which are generating the output. <?php print_r($sizeArray); //<-- shows an array print_r($sizeArraySORTED); //<-- shows "1" ?> Quote Link to comment https://forums.phpfreaks.com/topic/282183-unable-to-sort-array/#findComment-1449709 Share on other sites More sharing options...
vinny42 Posted September 16, 2013 Share Posted September 16, 2013 There are two print_r() statements which are generating the output I did see those, I didn't see the '1' danling at the bottom of the text :-) But that raises the question why the OP didn't look at asort() to see how it works... Quote Link to comment https://forums.phpfreaks.com/topic/282183-unable-to-sort-array/#findComment-1449710 Share on other sites More sharing options...
Psycho Posted September 16, 2013 Share Posted September 16, 2013 @Realistic Just in case you didn't follow Barand's response. The array asort function will sort the original variable - no need to assign the result to a variable. A couple other things. No need to define $sizeArray as an empty array and then define it again as an array with values. You only need to define an empty array first if you need to use the variable as an array in some way. For example, if you add values to the array using $sizeArray[] = 'foo'; $product_length = 2; $product_width = 5; $product_height = 3; // work out volume. $sizeArray = array($product_length, $product_width, $product_height); echo "Presort:<br><pre>" . print_r($sizeArray, 1) . "</pre>"; echo "<br><br><br>\n"; asort($sizeArray, SORT_NUMERIC); echo "Sorted:<br><pre>" . print_r($sizeArray, 1) . "</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/282183-unable-to-sort-array/#findComment-1449714 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.