-1

I'm new to php and I feel that this language has some really good set of sorting functions for arrays

I have an input array below containing the books , readers and price

$input[]=array("Harrypotter","John",50);
$input[]=array("Twilight","John",60);
$input[]=array("Harrypotter","Jack",80);
$input[]=array("Gonegirl","marion",90);
$input[]=array("Gonegirl","test",90);
$input[]=array("Gonegirl","John",90);
$input[]=array("Gonegirl","eliza",90);

I want to sort the array on first field in such a way that the book which repeats more number of times should come first , the expected O/p is something like this

$input[]=array("Gonegirl","marion",90);
$input[]=array("Gonegirl","test",90);
$input[]=array("Gonegirl","John",90);
$input[]=array("Gonegirl","eliza",90);
$input[]=array("Harrypotter","Jack",80);
$input[]=array("Harrypotter","John",50);
$input[]=array("Twilight","John",60);

I'm able to copy the first field in separate array and get the occurrence by using array_count_values and i'm able to get the expected output through array_multi_sort but it sorts only by alphabetical order of the first field .

Any efficient way of arriving at the solution would be helpful !

Cœur
  • 37,241
  • 25
  • 195
  • 267
Santhosh Pai
  • 2,535
  • 8
  • 28
  • 49

3 Answers3

1

This should work for you:

<?php

    $input = array();

    $input[]=array("Harrypotter","John",50);
    $input[]=array("Twilight","John",60);
    $input[]=array("Harrypotter","Jack",80);
    $input[]=array("Gonegirl","marion",90);
    $input[]=array("Gonegirl","test",90);
    $input[]=array("Gonegirl","John",90);
    $input[]=array("Gonegirl","eliza",90);

    function cmp($a, $b) {
        return strcmp($a[0], $b[0]);
    }

    usort($input, "cmp");

    print_r($input);

?>
Rizier123
  • 58,877
  • 16
  • 101
  • 156
1

You will first have to establish a count of how often each value occurs, then you can write a comparison function that sorts by those values:

$counts = array_count_values(array_map('current', $input));
usort($input, function (array $a, array $b) use ($counts) {
    return $counts[$b[0]] - $counts[$a[0]];
});
deceze
  • 510,633
  • 85
  • 743
  • 889
  • works like a charm tested for different inputs ,but it sorts in the ascending order . What does the call back function do ? – Santhosh Pai Nov 15 '14 at 19:09
  • See http://stackoverflow.com/a/17364128/476. Just reverse the `$a` and `$b` parameters if you want it the other way around. – deceze Nov 15 '14 at 19:13
  • @SanthoshPai You wanted to sort it that it ends with 80, 50, 60 and his answer ends with: 50, 80, 60? – Rizier123 Nov 15 '14 at 19:22
  • Hello @Rizier123 , i actually wanted only for maximum occurences of first field . – Santhosh Pai Nov 15 '14 at 19:26
0
array_multisort(array_map('count', $input), SORT_DESC, $input);

PS: From PHP Sort a multidimensional array by number of items

Update:

array_multisort(array_map(function ($fld) {return $fld[0];}, $input), SORT_ASC, $input);
Community
  • 1
  • 1
fortune
  • 3,361
  • 1
  • 20
  • 30