Namit
Namit

Reputation: 1322

Sorting a 2d array by numeric column value

I have an array with some values which is being declared on the fly:

// a for loop that finds elements from source
$array[] = array($fruit, $color, $shape, $price);

later i have another for loop which displays the items in the array:

for($j = 0; $j < sizeof($array); $j++) {
    echo $array[$j][0]." ".$array[$j][1]." ".$array[$j][2]." ".$array[$j][3]."
             ".$array[$j][4];
}

I am able to display all the information i want properly, however i want to sort the array before displaying based on the $price. e.g

Orange orange round £.6
Mango yellow oval £.9
Apple red round £.4

I want it in the following form

Apple red round £.4
Orange orange round £.6
Mango yellow oval £.9

Using uasort: Before :

Array ( [0] => Array ( [0] => Orange [1] => red [2] => round [3] => .6) 
        [1] => Array ( [0] => Mango [1] => yellow [2] => oval [3] => .9)
        [2] => Array ( [0] => Apple [1] => red [2] => round [3] => .4));

function provided:

function my_cmp_func($a, $b) {
   if($a[3] == $b[3]){
       return -1*strcmp($a[4], $b[4]); // another column added for comparison
   }
   return -1*strcmp($a[3], $b[3]);     
}

uasort($array, 'my_cmp_func');
print_r($array);

Upvotes: 1

Views: 133

Answers (5)

MDMalik
MDMalik

Reputation: 4001

If i was suppose to code this.

I would have made another array. Which would have received the value after sorting.

And then I would have displayed the output. I would suggest the bubble sort would be simple enough for this.

Cheers!

One of the example would be something like this

$numbers = array(1,3,2,5,2);  
$array_size = count($numbers);  
echo "Numbers before sort: ";  
for ( $i = 0; $i < $array_size; $i++ )  
   echo $numbers[$i];  
echo "n";  
for ( $i = 0; $i < $array_size; $i++ )  
{  
   for ($j = 0; $j < $array_size; $j++ )  
   {  
      if ($numbers[$i] < $numbers[$j])  
      {  

     //all the swapping variable here.....
         $temp = $numbers[$i];  
         $numbers[$i] = $numbers[$j];  
         $numbers[$j] = $temp;  
      }  
   }  
}  

echo "Numbers after sort: ";  
for( $i = 0; $i < $array_size; $i++ )  
   echo $numbers[$i];  
echo "n";  

Upvotes: 1

mishu
mishu

Reputation: 5397

you can do that using a custom comparing function

so you start by using the usort function (if you don't need to keep the indexes) so you will call it like this:

usort($array, 'my_cmp_func');

and define the comparing function as:

function my_cmp_func($a, $b)
{
    return strcmp($a[3], $b[3]);    
}

I defined the function using the strcmp because I don't know if the pound sign is part of the value or you just added it now.. but you can define the function the way you want

Edit: I added the sample here http://codepad.org/DxgFznzE to show that it works; and if you want to reverse the sort order just use this in the function:

return -1*strcmp($a[3], $b[3]);

instead of just the value you get from strcmp

Upvotes: 3

ronakg
ronakg

Reputation: 4212

Just call array_multisort($array); before your for loop.

array_multisort() can do many useful sorting things - see details here - http://www.php.net/manual/en/function.array-multisort.php

Also, your for loop can be simplified as -

foreach($array as $a) {
    echo $a[0] . " " . $a[1] . " " . $a[2] . " " . $a[3];
}

Upvotes: 0

JohnColvin
JohnColvin

Reputation: 2509

If you're not going to have repeat prices, use the price as the key of the array. Like:

$array[$price] = array($fruit, $color, $shape, $price);

Then sort by keys:

ksort($array);

Then do your output.

If you will have repeat prices, you could probably use array_walk or array_multisort.

Upvotes: 0

CFL_Jeff
CFL_Jeff

Reputation: 2719

Store it in an associative array, which will allow you to use the price as the key and easily print the items in ascending key order.

Upvotes: 0

Related Questions