原始数组:
$resolutions = array (
"Desktop monitor" => array (
//Standard 4:3
"800x600" => "800x600",
"1024x768" => "1024x768",
"1600x1200" => "1600x1200",
//Wide 16:10
"960x600" => "960x600",
"1280x800" => "1280x800",
"1440x900" => "1440x900",
"1680x1050" => "1680x1050",
"1920x1200" => "1920x1200",
),
"Apple" => array (
"DeviceX" => "2048x1536",
"DeviceY" => "1024x768",
),
);
通缉阵列:
$resolutions = array (
"Desktop monitor" => array (
"800x600" => "800x600",//Standard 4:3
"960x600" => "960x600",//Wide 16:10
"1024x768" => "1024x768",//Standard 4:3
"1280x800" => "1280x800",//Wide 16:10
"1440x900" => "1440x900",//Wide 16:10
"1600x1200" => "1600x1200",//Standard 4:3
"1680x1050" => "1680x1050",//Wide 16:10
"1920x1200" => "1920x1200",//Wide 16:10
),
"Apple" => array (
"DeviceY" => "1024x768",
"DeviceX" => "2048x1536",
),
);
我尝试了什么:
foreach ($resolutions as $screen => $resolution) {
foreach($resolution as $key => $val) {
$newarray[$key] = $row[$val];
}
array_multisort($newarray, SORT_DESC, $resolution);
}
我认为我已经结束了一半,但上面的代码给了我一些无意义(对我而言),即:首先是分辨率1024x768,然后是1280x800,然后是1440x900,最后是800x600。
答案 0 :(得分:1)
问题在于您按字母顺序排序。您需要计算每个分辨率的大小才能正确排序。这将需要编写一个自定义比较函数来计算分辨率大小并进行比较。
foreach ($resolutions as &$resolution)
uasort($resolution, function ($a, $b) { return array_product(explode('x', $a)) - array_product(explode('x', $b)); });
答案 1 :(得分:0)
这是一个专门针对Desktop monitor
数组键的快速黑客攻击。 ksort
将按数组键排序,但如果某些数字中间有x
,则排序不正确。这是我的解决方案:
ksort
对新数组进行排序array_walk
新数组用于创建看起来像原始您可以对原始数组中的其他数组执行相同的操作,例如Apple
使用值而不是键作为解析,并将array_walk
等放入循环中。
答案 2 :(得分:0)
只要看看你放在那里的东西,就有两点突出:
尝试:
foreach ($resolutions as $resolution) {
asort($resolution);
}