我想对数组进行排序,以便找到放在其中的最常见的单词

时间:2017-07-15 10:33:55

标签: php

我想对数组进行排序,以便找到放在其中最常见的单词: -

Array ( [0] => Array ( [0] => this [1] => burger [2] => is [3] => owsum ) [1] 
=> Array ( [0] => this [1] => burger [2] => is [3] => owsum ) [2] => Array ( 
[0] => love [1] => this [2] => burger ) [3] => Array ( [0] => love [1] => this 
[2] => burger ) [4] => Array ( [0] => kamaaal [1] => burger ) [5] => Array ( 
[0] => kamaaal [1] => burger ) [6] => Array ( [0] => this [1] => burger [2] => 
is [3] => owsum ) )

2 个答案:

答案 0 :(得分:0)

$max= array();
foreach ($arr as $key => $value){
    foreach ($value as $key2 => $value2){
        $index = $value2;
        if (array_key_exists($index, $max)){
            $max[$index]++;
        } else {
            $max[$index] = 1;
        }
    }
}
echo array_search(max($max),$max);

答案 1 :(得分:0)

$a = Array (
    '0' => Array ( '0' => 'this', '1' => 'burger', '2' => 'is', '3' => 'owsum', ) ,
    '1' => Array ( '0' => 'this', '1' => 'burger', '2' => 'is', '3' => 'owsum', ) ,
    '2' => Array ( '0' => 'love', '1' => 'this',  '2' => 'burger', ) ,
    '3' => Array ( '0' => 'love', '1' => 'this',  '2' => 'burger', ) ,
    '4' => Array ( '0' => 'kamaaal', '1' => 'burger', ) ,
    '5' => Array ( '0' => 'kamaaal', '1' => 'burger', ) ,
    '6' => Array ( '0' => 'this', '1' => 'burger', '2' => 'is', '3' => 'owsum', ) ,
);
// Merge all subarrays into one
$merged = call_user_func_array('array_merge', $a);

// Apply `array_count_values` to count number of occurencies
$count_values = array_count_values($merged);

// Sort this array preserving keys
arsort($count_values);

// Do whatever you want - iterate with foreach, get first element etc.