0

I have 3 arrays containing 3 pieces of info. Probably easier to show you, here:

    $dataPoints = array(
    array('1' => '33','2' => 'dave','3' => '367'),
    array('1' => '168','2' => 'susan','3' => '56788'),
    array('1' => '99','2' => 'tim','3' => '6')

foreach ($dataPoints as $key => $row) {
    $x[$key]  = $row['1'];
    $y[$key] = $row['2'];
    $z[$key] = $row['3'];   
}

$aaa = array_multisort($x, SORT_DESC, $y, SORT_ASC, $z, $dataPoints);


print_r($aaa);

I am trying to sort the lowest '3' (3rd column) value, then output all 3 answers for that array.

So, '6' is the lowest on the 3rd column. Then output '99', 'tim' & '6'.

What am I doing wrong?

samg
  • 3
  • 1
  • [Link](http://stackoverflow.com/questions/1345307/php-retrieve-minimum-and-maximum-values-in-a-2d-associative-array) check this – DS9 Sep 19 '13 at 13:30

3 Answers3

0

The sort operation is being performed on the inidivdual arrays ($x, $y, $z). If you look at these arrays you will see they have been sorted.

$aaa = true as this means the array_multisort was successful.

Buchow_PHP
  • 410
  • 3
  • 10
0

According to The docs, array_multisort returns TRUE or FALSE. So your array will be modified directly.

The correct code is :

$dataPoints = array(
array('1' => '33','2' => 'dave','3' => '367'),
array('1' => '168','2' => 'susan','3' => '56788'),
array('1' => '99','2' => 'tim','3' => '6')
);

foreach ($dataPoints as $key => $row) {
    $x[$key]  = $row['1'];
    $y[$key] = $row['2'];
    $z[$key] = $row['3'];   
}

$aaa = array_multisort($x, SORT_DESC, $y, SORT_ASC, $z, SORT_ASC, $dataPoints);

print_r($aaa); //TRUE or FALSE
print_r($dataPoints); //The data sorted

Also note that you are sorting by the first column, then the second and then the third (in case of ties). If you want to sort by the third column first, you should use this :

$aaa = array_multisort($z, SORT_ASC, $x, SORT_DESC, $y, SORT_ASC, $dataPoints);
xurei
  • 1,057
  • 8
  • 22
  • Ooops, sorry, home made print_r method (faster to write :-D) – xurei Sep 19 '13 at 13:40
  • might be stupid, but how to I output just 1 of the values, or output them separately, not in the array? I just get this: Array ( [0] => Array ( [1] => 6 [2] => 1000 [3] => 7.38 ) [1] => Array ( [1] => 10 [2] => 300 [3] => 3.61 ) [2] => Array ( [1] => 14 [2] => 630 [3] => 2.58 ) ) – samg Sep 19 '13 at 13:42
  • Ta. Marked as answered too. – samg Sep 19 '13 at 13:49
0

I believe this is what you want, after the usort, the first entry in $datapoints will hold the record you are looking to find. Of course, you can modify the function in usort to sort however you please.

<?php
$dataPoints = array(
array('1' => '33','2' => 'dave','3' => '367'),
array('1' => '168','2' => 'susan','3' => '56788'),
array('1' => '99','2' => 'tim','3' => '6')
);

usort($dataPoints, function($a, $b){
    if ($a[3] == $b[3]) {
        return 0;
    }
    return ($a[3] < $b[3]) ? -1 : 1;
});

print_r($dataPoints[0]);

Here is a link to a working example http://3v4l.org/XJU9t

bubba
  • 3,839
  • 21
  • 25