请帮助我,我需要合并多个数组,然后按数组值计数对其进行排序。下面是问题所在:

$array1 = array("abc", "def", "ghi", "jkl", "mno");
$array2 = array("mno", "jkl", "mno", "ghi", "pqr", "stu");
$array3 = array_merge($array1, $array2);
$array4 = ???

print_r($array4);

我想要像这样的$array4的返回:
Array
(
[0] => mno
[1] => ghi
[2] => jkl
[3] => abc
[4] => def
[5] => pqr
[6] => stu
)

最佳答案

你可以做:

$array1 = array("abc", "def", "ghi", "jkl", "mno");
$array2 = array("mno", "jkl", "mno", "ghi", "pqr", "stu");
$array3 = array_merge($array1, $array2);

// get the array of count.
$array4 = array_count_values($array3);

// sort it in reverse order.
arsort($array4);

// extract just the keys.
$array4 = array_keys($array4);

Working example

关于php - 合并多个数组,然后按数组值计数排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2736635/

10-10 15:53