0

Possible Duplicate:
How do I sort a multidimensional array in php

Array ( [0] => Array ( 
                    [card_name] => CardA 
                    [str] => 10 
                    [def] => 10 
                    [desc] => - Recover 150 points of vitality 
                              - Attack twice" 
                    [qty] => 5 
                ) 
    [1] => Array ( 
                    [card_name] => CardD 
                    [str] => 40 
                    [def] => 40 
                    [desc] => - Investigate enemy's weakpoint 
                    [qty] => 3 
                ) 
    [2] => Array ( [card_name] => CardG 
                   [str] => 35 
                   [def] => 20 
                   [desc] => 
                   [qty] => 1 
                ) 
    [3] => Array ( 
                   [card_name] => CardH 
                   [str] => 25 
                   [def] => 30 
                   [desc] => 
                   [qty] => 1 
                ) 
    [4] => Array ( 
                   [card_name] => CardI 
                   [str] => 15 
                   [def] => 40 
                   [desc] => - Enhance strength 
                   [qty] => 1 
                ) 
    [5] => Array ( 
                   [card_name] => CardJ 
                   [str] => 5 
                   [def] => 50 
                   [desc] => - Make silence 
                   [qty] => 3 
                   ) 
    )

I have a simple question about sorting arrays. I just want to sort the array in either by str or def in either asc or desc. The examples in the php.net is a bit confusing and I was wondering if anyone can solve this small dilemma.

I know I should be using array_multi_sort for this.

Thanks.

Community
  • 1
  • 1
Mr A
  • 1,205
  • 4
  • 19
  • 49

4 Answers4

0

Try php usort - http://www.php.net/manual/en/function.usort.php

I've not tried and run the code but it'll be something below:

function cmp($a, $b)
{
   return strcmp($a['def'],$b['def']) 
}

$a = yourArray[];

usort($a, "cmp");

This will iterate through each element and pass the array element as a parameter and will use your custom function to sort.

Mahbub
  • 195
  • 3
  • 8
0

Use the usort function. It will be something like this in your case :

function compare($valueA, $valueB) {
    if ($valueA['str'] > $valueb['str']) {
        return 1;
    } else if ($valueA['str'] < $valueb['str']) {
        return -1;
    }
    return 0;
}

usort($yourArray, "compare");
Jean-Philippe Bond
  • 8,829
  • 2
  • 32
  • 56
0

usort() and strnatcmp() is your solution:

<?php
    function build_sorter($key) {
        return function ($a, $b) use ($key) {
            return strnatcmp($a[$key], $b[$key]);
        };
    }

    usort($array, build_sorter('def'));
    print_r($array);
?>

It will sort all arrays by your defined key.

Mihai Iorga
  • 36,863
  • 13
  • 100
  • 102
0

Array multisort requires you to build an array holding the values of the keys you want to sort by to do what you want, but with the same first dimension keys of the original array.

Thus if you make an array which holds 0 => 10, 1 => 5... etc where those are the values for str or def for keys 0 and 1, then you can do

foreach($originalArray as $key => $record) {
    $keyValuesArray[$key] = $record['str'];
}

array_multisort($keyvaluesArray, SORT_ASC, $originalArray);

And original array would be modified to be sorted.

As for what you should be doing, I don't think array multisort is "the way" to do it, but is not a terrible way of doing it. Other solutions might be slightly more efficient.

fd8s0
  • 1,781
  • 1
  • 13
  • 28