在输出时,我想要一个每个唯一高度的行的数组。如果输入中存在具有相同ext
和height
(如0,6)的行,则只需获取第一个值。如果有height
的{{1}}优先于jpg
而非png
优先于gif
的行(3,9,10)。
这是输入数组:
[input] => Array
(
[0] => Array
(
[ext] => jpg
[height] => 800
[md5] => 87167a1952911df64a3b1a423c95b32b
[id] => ddf
)
[1] => Array
(
[ext] => png
[height] => 330
[md5] => 87167a1952911df64a3b1a423c95b32b
[id] => 117
)
[2] => Array
(
[ext] => jpg
[height] => 330
[md5] => 8d167a1952ds1df64a3b1a423c95b32b
[id] => 24
)
[3] => Array
(
[ext] => gif
[height] => 150
[md5] => 4a4d993ed7bd7d467b27af52d2aaa800
[id] => 68
)
[4] => Array
(
[ext] => jpg
[height] => 1024
[md5] => 912ec803b2ce49e4a541068d495ab570
[id] => 78
)
[5] => Array
(
[ext] => png
[height] => 1024
[md5] => 6a204bd89f3c8348afd5c77c717a097a
[id] => lp
)
[6] => Array
(
[ext] => jpg
[height] => 800
[md5] => dce4f98878b0c302cb3de0dcd27d8bc8
[id] => cd
)
[7] => Array
(
[ext] => png
[height] => 800
[md5] => ace4f98878b0c302cb3de0dcd27d8bc8
[id] => mmc
)
[8] => Array
(
[ext] => png
[height] => 430
[md5] => gce4f98878b0c302cb3de0dcd27d8bc8
[id] => 115
)
[9] => Array
(
[ext] => png
[height] => 150
[md5] => xce4f98878b0c302cb3de0dcd27d8bc8
[id] => 4568
)
[10] => Array
(
[ext] => jpg
[height] => 150
[md5] => cce4f98878b0c302cb3de0dcd27d8bc8
[id] => 8777
)
[11] => Array
(
[ext] => gif
[height] => 400
[md5] => kke4f98878b0c302cb3de0dcd27d8bc8
[id] => 877
)
[12] => Array
(
[ext] => bmp
[height] => 500
[md5] => 89e4f98878b0c302cb3de0dcd27d8bc8
[id] => 857
)
[12] => Array
(
[ext] => jpg // no height row
[md5] => rde4f98878b0c302cb3de0dcd27d8bc8
[id] => a57
)
到目前为止,这是我丑陋的代码:
foreach ($input as $row) {
if (!empty($row['height'])) {
$vo['height'] = $row['height'];
$vo['md5'] = $row['md5'];
$vo['ext'] = $row['ext'];
$help_array[$row['height']][$row['ext']][] = $vo; // creates help array grouped by height
}
}
$i = 0;
foreach ($help_array as $helps) {
foreach ($helps as $k => $help) {
if ($k == 'jpg') { // best format to use
$output[$i] = $help[0];
break;
}
}
if (empty($output[$i])) { // if still empty use png
foreach ($helps as $k => $help) {
if ($k == 'png') {
$output[$i] = $help[0];
break;
}
}
}
if (empty($output[$i])) { // if there is no jpg or png take gif
foreach ($helps as $k => $help) {
if ($k == 'gif') {
$output[$i] = $help[0];
break;
} else { // or take what left
$output[$i] = $help[0];
break;
}
}
}
++$i;
}
print_r($output);
它有太多行:-)这应该是输出数组(可选择没有ext
列,因为它不再需要)按height
排序:
[output] => Array
(
[0] => Array
(
[ext] => jpg
[height] => 150
[md5] => cce4f98878b0c302cb3de0dcd27d8bc8
)
[1] => Array
(
[ext] => jpg
[height] => 330
[md5] => 8d167a1952ds1df64a3b1a423c95b32b
)
[2] => Array
(
[ext] => gif
[height] => 400
[md5] => kke4f98878b0c302cb3de0dcd27d8bc8
)
[3] => Array
(
[ext] => png
[height] => 430
[md5] => gce4f98878b0c302cb3de0dcd27d8bc8
)
[4] => Array
(
[ext] => bmp
[height] => 500
[md5] => 89e4f98878b0c302cb3de0dcd27d8bc8
)
[5] => Array
(
[ext] => jpg
[height] => 800
[md5] => 87167a1952911df64a3b1a423c95b32b
)
[6] => Array
(
[ext] => jpg
[height] => 1024
[md5] => 912ec803b2ce49e4a541068d495ab570
)
答案 0 :(得分:1)
这是一个可以解决问题的简单功能
function filterAndSort($input){
$output = array();
$extensionsWeght = array(
'jpg' => 0,
'png' => 1,
'gif' => 2
);
foreach ($input as $v) {
//Edit 1: skip if no hieght is present
if(!isset($v['height'])){
continue;
}
if (!isset($output[$v['height']])) {
//If it's a new height, add it
$output[$v['height']] = $v;
} else {
//else, choose the prefered ext [if they have the same ext, the first entry is already choosen]
if ($extensionsWeght[$output[$v['height']]['ext']] > $extensionsWeght[$v['ext']]) {
$output[$v['height']] = $v;
}
}
}
//Sort the ouput by height key
// usort($output, function($a, $b) {
// return $a['height'] - $b['height'];
//});
//Edit 2 :
//For sorting the array, as it is indexed with the height key,
// you can use the built in functions to sort it. (ksort for this case)
ksort($output);
return array_values($output);
}
您可以找到测试here