zero-perfoliate
zero-perfoliate

Author Topic: Sorting a 2d array  (Read 328 times)

Offline vaillant

  • New PHP Members
  • Posts: 1
  • Karma: +0/-0
Sorting a 2d array
« on: January 29, 2009, 01:35:31 PM »
Hi

$catalog is a 2d array.

When I do:

foreach($catalog as $key => $value)
               {
                  echo $key."=>".$catalog[$key]['desc']." ".$catalog[$key]['price'];
               }

I get:

101=>AA batteries (pack of 2) 2.90
102=>AA batteries (pack of 4) 5.49
103=>Backpack (black) 69.99
104=>Money belt with 6 compartments 13.49
105=>Haversack (red) 199.99
106=>Swiss Army knife (6 blades) 24.00
107=>Duffel bag (steel gray) 28.50

When I do:

                                        sort($catalog);
               foreach($catalog as $key => $value)
               {
                  echo $key."=>".$catalog[$key]['desc']." ".$catalog[$key]['price'];
               }

I get:

0=>AA batteries (pack of 2) 2.90
1=>AA batteries (pack of 4) 5.49
2=>Backpack (black) 69.99
3=>Duffel bag (steel gray) 28.50
4=>Haversack (red) 199.99
5=>Money belt with 6 compartments 13.49
6=>Swiss Army knife (6 blades) 24.00

First thing: Can someone please explain why it loses the $keys (i.e 101 becomes 0, 102 becomes 1...)??

Second thing: My problem is that I need to sort it so it's in ascending order according to the price (and not according to the item description like it did above).

This is what I need:

101=>AA batteries (pack of 2) 2.90
102=>AA batteries (pack of 4) 5.49
104=>Money belt with 6 compartments 13.49
106=>Swiss Army knife (6 blades) 24.00
107=>Duffel bag (steel gray) 28.50
103=>Backpack (black) 69.99
105=>Haversack (red) 199.99

Hope it makes sense...thanks for your help.
« Last Edit: January 29, 2009, 01:38:29 PM by vaillant »

Offline PhPHelper

  • http://Digiscapers.com
  • Full Member
  • PHP Problem Solvers
  • ****
  • Posts: 179
  • Karma: +50/-0
    • Bad Apple Mail
Re: Sorting a 2d array
« Reply #1 on: February 10, 2009, 05:29:17 PM »
First thing: Can someone please explain why it loses the $keys (i.e 101 becomes 0, 102 becomes 1...)??

Arrays are 0 indexed meaning they start with 0 not 1 to sort the use you can use the command sort

Code: [Select]
<?php

$fruits 
= array("lemon""orange""banana""apple");
sort($fruits);
foreach (
$fruits as $key => $val) {
    echo 
"fruits[" $key "] = " $val "\n";
}

?>


The above example will output:

fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange


other commands that can be used with arrays:

    *  arsort() - Sort an array in reverse order and maintain index association
    * asort() - Sort an array and maintain index association
    * ksort() - Sort an array by key
    * rsort() - Sort an array in reverse order
    * usort() - Sort an array by values using a user-defined comparison function
    * uksort() - Sort an array by keys using a user-defined comparison function
    * array_multisort() - Sort multiple or multi-dimensional arrays
    * krsort() - Sort an array by key in reverse order
    * natsort() - Sort an array using a "natural order" algorithm
    * natcasesort() - Sort an array using a case insensitive "natural order" algorithm

have a look at the php manual: http://uk3.php.net/sort

 

zero-perfoliate