2010-04-29 6 views
6

Por favor, ayúdenme, necesito unir múltiples matrices y luego ordenarlas por el recuento de valores de matriz. A continuación se muestra el problema:Fusionando múltiples matrices luego ordenando por valor de matriz cuenta

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

print_r($array4); 

Quiero que los rendimientos de $array4 así:

Array 
(
[0] => mno 
[1] => ghi 
[2] => jkl 
[3] => abc 
[4] => def 
[5] => pqr 
[6] => stu 
) 

Respuesta

11

que puede hacer:

$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

+1

Perfectttttttttttt –

+0

perfecto, muchas gracias mucho – Sofyan

+0

por cierto, ¿es posible merg e 3 o más matrices? – Sofyan

Cuestiones relacionadas