我有以下数组,我想按每个单词的数量对其进行排序。
Array ( [0] => Array ( [test] => 0 )
[1] => Array ( [test1] => 1296 )
[2] => Array ( [test2] => 1046 )
[3] => Array ( [test3] => 1171 )
[4] => Array ( [test4] => 857 )
[5] => Array ( [test5] => 1051 )
[6] => Array ( [test6] => 929 )
[7] => Array ( [test7] => 986 )
我想制作
Array ( [0] => Array ( [test1] => 1296 )
[1] => Array ( [test3] => 1171 )
等...
答案 0 :(得分:1)
尝试以下功能可能会对您有所帮助,
使用以下给定函数对第二个元素的数组进行排序。
function sort_array_second_element($array) {
$temp = array();
foreach ($array as $key => $value) {
foreach ($value as $key_new => $value_new) {
$temp[] = $value_new;
}
}
rsort($temp);
$new_temp = array();
foreach ($temp as $value) {
foreach ($array as $value_new) {
foreach ($value_new as $key_new => $value_temp) {
if ($value == $value_temp) {
$new_temp[] = $value_new;
}
}
}
}
return $new_temp;
}
在给定函数中简单传递当前数组,如下所示:
$arr = array(
array("test" => 0),
array("test1" => 1296),
array("test2" => 1046),
array("test3" => 1171),
);
$sorted_arr = sort_array_second_element($arr);
print_r($sorted_arr);
这将生成您想要的相同输出。