user2037290
user2037290

Reputation: 93

PHP sorting of multi-dimensional array by price value

Say I have a multi-dimensional array set up as follows:

$matrix[1][1]=4.54;        $matrix[2][1]="apples";  $matrix[3][1]="coles";
$matrix[1][2]=7.2140230;   $matrix[2][2]="apples";  $matrix[3][2]="safeway";
$matrix[1][3]=15.56;       $matrix[2][3]="oranges"; $matrix[3][3]="coles";
$matrix[1][4]=2.34;        $matrix[2][4]="bananas"; $matrix[3][4]="safeway";
$matrix[1][5]=27.98;       $matrix[2][5]="grapes";  $matrix[3][5]="coles";
$matrix[1][6]=17.68493403; $matrix[2][6]="oranges"; $matrix[3][6]="safeway";

And I wish to re-arrange by the pricing information which I've stored under the first 1st column, so that the new order of $matrix would be:

$matrix[1][1]=2.34;        $matrix[2][1]="bananas"; $matrix[3][1]="safeway";
$matrix[1][2]=4.54;        $matrix[2][2]="apples";  $matrix[3][2]="coles"; 
$matrix[1][3]=7.2140230;   $matrix[2][3]="apples";  $matrix[3][3]="safeway";
$matrix[1][4]=15.56;       $matrix[2][4]="oranges"; $matrix[3][4]="coles";
$matrix[1][5]=17.68493403; $matrix[2][5]="oranges"; $matrix[3][5]="safeway";
$matrix[1][6]=27.98;       $matrix[2][6]="grapes";  $matrix[3][6]="coles";

What would be the best way to achieve this? I've read other questions about sorting multi-dimensional arrays but have had trouble implementing because those examples seemed to have associative arrays with keys and elements, whereas I am just using the different numbers to store each piece of data. I would prefer not to change the way I am storing data in the array as the actual script is quite long and complex and so this would involve a lot of re-work.

I am completely new to PHP so my apologies if I am missing something obvious here. Thanks for your help.

EDIT: Thanks everyone for your advice, scessors code is exactly what I needed. Halfer - to your 1st question - Yes, 2nd post - good point, I will implement this. Thanks again everyone!

Upvotes: 2

Views: 1121

Answers (3)

user1646111
user1646111

Reputation:

Using my_uksort provided by Adam Backstrom

<?php
function my_uksort($a, $b) {
    global $matrix;
    asort($matrix[1]); 
    return $matrix[1][$a] < $matrix[1][$b] ? -1 : 1;
}

uksort($matrix[2], 'my_uksort');
uksort($matrix[3], 'my_uksort');

print_r($matrix);
?>

DEMO

Upvotes: 0

scessor
scessor

Reputation: 16115

If it's no problem that the sorted array begins with zero, you can use array_multisort:

array_multisort(
    $matrix[1], SORT_ASC, SORT_NUMERIC,
    $matrix[2], SORT_ASC, SORT_STRING,
    $matrix[3], SORT_ASC, SORT_STRING
);

Also see my example.

Upvotes: 2

DEzra
DEzra

Reputation: 3038

I believe that phps array_multisort will do it for you:

Heres an example from docs:

<?php
$ar1 = array(10, 100, 100, 0);
$ar2 = array(1, 3, 2, 4);
array_multisort($ar1, $ar2);

var_dump($ar1);
var_dump($ar2);
?>

In this example, after sorting, the first array will contain 0, 10, 100, 100. The second array will contain 4, 1, 2, 3. The entries in the second array corresponding to the identical entries in the first array (100 and 100) were sorted as well.

From: http://php.net/manual/en/function.array-multisort.php

Upvotes: 1

Related Questions