我有一个问题。我必须得到5张率最高的图像。我有一个表key = image_id和value =平均评级。
下面是此数组的print_r
Array ( ['5'] => 5.00 ['4'] => 3.05 ['12'] => 3.00 ['11'] => 4.00 ['21'] => 2.11 ['53'] => 4.44 )
例如
['5'] => 5.00
表示id ='5'的img的评级为5.00
预期输出2阵列($ id和$ rating)
Array ( [0] => '5' [1] => '53' [2] => '11' [3] => '4' [4] => '12' )
Array ( [0] => '5.00' [1] => '4.44' [2] => '4.00' [3] => '3.05' [4] => '3.00' )
你可以帮帮我吗?
答案 0 :(得分:2)
您还可以避免使用array_keys();和array_values();
等功能制作2个独立的数组// Original array
$array = array(
5 => 5.00,
4 => 3.05,
12 => 3.00,
11 => 4.00,
21 => 2.11,
53 => 4.44
);
// Sort array & maintain keys
arsort($array);
// Now get the first 5 elements, keeping the keys
$array = array_slice($array, 0, 5, true);
// IDs
print_r(array_keys($array));
// Ratings
print_r(array_values($array));
答案 1 :(得分:0)