Jump to content

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>";
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.