Jump to content

unable to sort array


jason310771

Recommended Posts

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);
Link to comment
https://forums.phpfreaks.com/topic/282183-unable-to-sort-array/
Share on other sites

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"
?>

@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>";

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.