0

I have an array stored in a variable $data. The array has names in the first row and a value in the second row. The array is very big so I need a way to take the five highest values from it and the name from those value. For example I have this array:

[0]=>
    array(1447) {
      [1]=>
      array(3) {
        [0]=>
        string(11) "Cris"
        [2]=>
        string(1) "11"
      }
      [2]=>
      array(3) {
        [0]=>
        string(7) "Alan"
        [2]=>
        string(1) "28"
      }
      [3]=>
      array(3) {
        [0]=>
        string(6) "Alex"
        [2]=>
        string(1) "50"
      }
      [4]=>
      array(3) {
        [0]=>
        string(6) "Zone"
        [1]=>
        string(1) "22"
      }
      [5]=>
      array(3) {
        [0]=>
        string(6) "Ana"
        [2]=>
        string(1) "1"
      }
      [6]=>
      array(3) {
        [0]=>
        string(6) "Fisca"
        [1]=>
        string(1) "5"
      }

In this case I should display: Alex 50, Alan 28, Zone 22, Cris 11 and Fisca 5. I tried to find a solution but I don't know how should I make a top of array values. Can you help me please? Thank you in advance.

Alan
  • 213
  • 1
  • 13
  • Have a look here: http://stackoverflow.com/q/2699086/3129708 – postrel Jul 03 '16 at 17:04
  • Thank you but I want to display only the five highest values...as plain text and the specific number besides it. I don't want to sort the array...does it make sense? Sorry I am new to backend. – Alan Jul 03 '16 at 17:07
  • The task flow would be: 1. Sort the array 2. Extract 5 elements. – postrel Jul 03 '16 at 17:12
  • first you should put it into an SQL database, then you should ask the SQLdb to sort it for you, then you should take it out of the db again. like this!: `function lolphpsort(array $arr):array{$db=new PDO('sqlite::memory','','',array(PDO::ATTR_EMULATE_PREPARES => false,PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));$db->exec('CREATE TABLE tbl (s TEXT,i INTEGER);');$stm=$db->prepare('INSERT INTO tbl VALUES (?,?);');foreach($arr as $a){$stm->execute(array($a[0],$a[2]));}return $db->query('SELECT * FROM tbl ORDER BY i DESC LIMIT 5')->fetchAll(PDO::FETCH_ASSOC); }` – hanshenrik Jul 03 '16 at 17:46

4 Answers4

0

First sort your array and then slice the top 5:

DEMO

usort($data, function ($a, $b) {
    return $b[1] - $a[1];
});   //Sort your array

$resultant = array_slice($data,0,5);  //Pick top 5

Note: If your index used for comparison differs, then change your return statement to:

return (isset($b[1])?$b[1]:$b[2]) - (isset($a[1])?$a[1]:$a[2]);

Example:

<?php

$data = [
['Cris', 11],
['Alan', 28],
['Alex', 50],
['Zone', 22],
['Ana', 1]
];

usort($data, function ($a, $b) {
    return $b[1] - $a[1];
});

print_r(array_slice($data,0,5));

Result:

Array
(
    [0] => Array
        (
            [0] => Alex
            [1] => 50
        )

    [1] => Array
        (
            [0] => Alan
            [1] => 28
        )

    [2] => Array
        (
            [0] => Zone
            [1] => 22
        )

    [3] => Array
        (
            [0] => Cris
            [1] => 11
        )

    [4] => Array
        (
            [0] => Ana
            [1] => 1
        )

)

DEMO

Thamilhan
  • 13,040
  • 5
  • 37
  • 59
0

You can use this to get the 5 highest values of your array:

<?php
function compare($a, $b) {
    if ($a[1] == $b[1]) {
        return 0;
    }

    return ($a[1] > $b[1]) ? -1 : 1;
}

usort($theBigArray, "compare");

$fiveHighestValues = array_slice($theBigArray, 0, 5);
?>

($theBigArray being your array)

And then, you can loop through the $fiveHighestValues var to display the five elements as you want, e.g.:

<?php
foreach($fiveHighestValues as $value) {
    echo $value[0] .' has the value '. $value[1];
    // output: Alex has the value 50
}
?>
Nikkow
  • 56
  • 1
  • 4
0

I would first sort the array like this

function mySort($a, $b) {
    return $b[1] - $a[1];
}
usort($arr, 'mySort');

Then you can just pop the first 5 values - they are the highest. You've written that 'The array has names in the first row and a value in the second row.', but I can see that for 'Zone' the index '1' is set not '2', so in your sorting function you might need to add some simple checking, maybe something like this:

function mySortWithCheck($a, $b) {
   if (isset($b[1])) {
       $valB = $b[1]; 
   } else {
       $valB = $b[2];
   }

   if (isset($a[1])) {
       $valA = $a[1]; 
   } else {
       $valA = $a[2];
   }

   return $valB - $valA;
}
Canu667
  • 175
  • 1
  • 5
0
<?php
$data = array(
           array("Cris", "11"),
           array("Alan",  "28"),
           array("Alex","50"),
           array("Zone","22"),
           array("Ana","1")
        );

var_dump($data);

function custom_array_sort($a, $b) {
    return $b[1] - $a[1];
}

usort($data,'custom_array_sort');

$sorted = array_slice($data,0,5);
var_dump($sorted);

?>
Ravi Roshan
  • 570
  • 3
  • 14