2017-03-01 66 views
1

输出时,我想要一个具有行的数组,每个唯一高度为。如果输入中有相同的行extheight(如0,6),则取第一个值。如果存在具有相同的行的行,则优先于优先jpg优于gif优先于其余(3,9,10)。php - 根据首选的行值组简化从多维数组中选择

这是输入数组:

[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); 

它有许多TOOOO线:-)这应该是输出阵列(可选地没有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 
      ) 
+1

如果没有指定'height'是,如在输入 –

+0

的最后一项如果没有'高度'跳过该行。参见'if(!empty($ row ['height'])){' –

回答

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

+0

'而不是'usort',使用'ksort'。然后用'return array_values($ output);' –

+0

删除键你能解释我为什么吗?,它是否更快? –

+3

可能,但它比使用用户定义的比较功能更短,更简单。 –