0

I want to make an array sort method so you can sort some info via price/ID number etc.

My array is like this:

[1] 1002234985, $123.00, ITEM DESCRIPTION #1

[2] 1034234985, $143.70, ITEM DESCRIPTION #2

[3] 1002467455, $133.06, ITEM DESCRIPTION #3

[4] 1564334985, $883.11, ITEM DESCRIPTION #4

I want to sort the array by price, but have the descriptions and ID numbers match up. How can I do this?

Thanks!

john_science
  • 6,325
  • 6
  • 43
  • 60

3 Answers3

1

write the compare function (that receives two elements and returns which one is bigger) and use usort to use it

An example:

$arr = array(
              array(1002234985, '125.00', 'ITEM DESCRIPTION'), 
              array(1002234986, '124.00', 'ITEM DESCRIPTION'), 
              array(1002234987, '123.00', 'ITEM DESCRIPTION')
);
function mycomp($itm1, $itm2){
    if($itm1[1] > $itm2[1]){
        return 1;
    }
    else if($itm1[1] < $itm2[1]){
        return -1;
    }
    else{
        return 0;
    }
}
usort($arr, 'mycomp');
print_r($arr);
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
0

Here is an example of sorting a multidimensional array using PHP's usort.

// Change this to the index of the key you wish to sort by
$sortBy = 0;

$myArray = array(
  array(1002234985, '$123.00', 'ITEM DESCRIPTION #1'),
  array(1034234985, '$143.70', 'ITEM DESCRIPTION #2'),
  array(1002467455, '$133.06', 'ITEM DESCRIPTION #3'),
  array(1564334985, '$883.11', 'ITEM DESCRIPTION #4')
];

usort($myArray, 'cmp');

function cmp($a, $b){
  if ($a[$sortBy] == $b[$sortBy]) {
    return 0;
  }
  return ($a[$sortBy] < $b[$sortBy]) ? -1 : 1;
}
Pastor Bones
  • 7,183
  • 3
  • 36
  • 56
0

Please use this below function:

function sortArrayByTwoKeys($data, $k1, $k2, $order='ASC'){
    if($order == 'DESC'){
        usort($data, function (array $a, array $b) {
            return [$b['score'], $b['name']] <=> [$a['score'], $a['name']];
        });
    } elseif($order == 'ASC_K2_DESC_K1'){
        usort($data, function (array $a, array $b) {
            return [$b['score'], $a['name']] <=> [$a['score'], $b['name']];
        });
    } elseif($order == 'ASC_K1_DESC_K2'){
        usort($data, function (array $a, array $b) {
            return [$a['score'], $b['name']] <=> [$b['score'], $a['name']];
        });
    } else {//ASC
        usort($data, function (array $a, array $b) {
            return [$a['score'], $a['name']] <=> [$b['score'], $b['name']];
        });
    }
    return $data;
}
//Example 1
$data = [
['id' => 111, 'score' => 5, 'name' => 'xyz'],
['id' => 222, 'score' => 3, 'name' => 'ddd'],
['id' => 333, 'score' => 2, 'name' => 'xyz'],
['id' => 444, 'score' => 3, 'name' => 'fff'],
['id' => 555, 'score' => 5, 'name' => 'acb'],
['id' => 666, 'score' => 6, 'name' => 'ttt'],
['id' => 777, 'score' => 6, 'name' => 'sss'],
['id' => 888, 'score' => 6, 'name' => 'aaa']
];
$data = sortArrayByTwoKeys($data, 'score', 'name', 'ASC_K2_DESC_K1');
echo '<pre>';print_r($data);echo '</pre>';

Output will be: Array ( [0] => Array ( [id] => 888 [score] => 6 [name] => aaa )

[1] => Array
    (
        [id] => 777
        [score] => 6
        [name] => sss
    )

[2] => Array
    (
        [id] => 666
        [score] => 6
        [name] => ttt
    )

[3] => Array
    (
        [id] => 555
        [score] => 5
        [name] => acb
    )

[4] => Array
    (
        [id] => 111
        [score] => 5
        [name] => xyz
    )

[5] => Array
    (
        [id] => 222
        [score] => 3
        [name] => ddd
    )

[6] => Array
    (
        [id] => 444
        [score] => 3
        [name] => fff
    )

[7] => Array
    (
        [id] => 333
        [score] => 2
        [name] => xyz
    )

)

Jitendra Rajput
  • 123
  • 1
  • 5