2

Here's an problem where i have to find the top book and the number of readers for each book in php . The array is something as below

$input=array();
$input[]=array("Harrypotter","John");
$input[]=array("Twilight","Jack");
$input[]=array("Twilight","John");
$input[]=array("Harrypotter","Jack");
$input[]=array("Gonegirl","marion");
$input[]=array("Gonegirl","marion");
$input[]=array("Gonegirl","John");
$input[]=array("Gonegirl","eliza");

i can find the top book by copying the book names onto a separate array say temparray and use the following functions

$temparray = array_count_values($temparray);
arsort($temparray);

but i'm not able to figure out on the logic how to get the number of readers for each book,the reader names may repeat so we have to eliminate the repeated ones .Any quick way to sort the thing would be helpful.

Kevin
  • 41,694
  • 12
  • 53
  • 70
Santhosh Pai
  • 2,535
  • 8
  • 28
  • 49

2 Answers2

4

You could get each book name first to create a flat array, then apply the count. Example:

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

$input = array_map('unserialize', array_unique(array_map('serialize', $input)));
// remove dups
$temparray = array_map(function($book_name){
    return $book_name[0]; // get book names
}, $input);
$temparray = array_count_values($temparray); // then apply the counting
arsort($temparray);

print_r($temparray); // Array ( [Gonegirl] => 3 [Twilight] => 2 [Harrypotter] => 2 )
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • May be two `marion` values wasn't mistake? ;) `the reader names may repeat so we have to eliminate the repeated ones` – vp_arth Nov 13 '14 at 07:34
  • @vp_arth i could only guess on my side, lets just wait if the OP really intend it to be that way – Kevin Nov 13 '14 at 07:36
  • The user names do repeat ,i'm trying to apply the logic to other problem like i have to sort the top url where the same user can hit the same website many times – Santhosh Pai Nov 13 '14 at 08:27
  • @SanthoshPai oh okay, so it can repeat and needed to be counted as well – Kevin Nov 13 '14 at 08:42
  • @Ghost : Its the other way around,the repeated users should not be counted . I didnt understand on how to do it . – Santhosh Pai Nov 13 '14 at 09:18
  • @SanthoshPai oh okay, i revised it a little bit to handle that – Kevin Nov 13 '14 at 09:24
1
$input=array();
$input[]=array("Harrypotter","John");
$input[]=array("Twilight","Jack");
$input[]=array("Twilight","John");
$input[]=array("Harrypotter","Jack");
$input[]=array("Gonegirl","marion");
$input[]=array("Gonegirl","marion");
$input[]=array("Gonegirl","John");
$input[]=array("Gonegirl","eliza");

$hash = array();
$sorted = array();
array_walk($input, function ($v) use (&$hash, &$sorted) {
    $book = $v[0];
    if (!in_array(implode('_', $v), $hash)) {
        $hash[] = implode('_', $v);
        !isset($sorted[$v[0]])?$sorted[$v[0]]=1:$sorted[$v[0]]++;
    }
});
arsort($sorted);
print_r($sorted);
vp_arth
  • 14,461
  • 4
  • 37
  • 66