如何让这个PHP代码更短?

时间:2013-09-24 14:57:13

标签: php loops

我有以下PHP代码可以使用,但它太长而且阅读麻烦......

// get the row
 if ($taktArticle[0]['t1position3'] > 3 AND $taktArticle[0]['t1position3'] < 7  ) {
   $row = "row1";
 }

 if ($taktArticle[0]['t1position3'] > 7 AND $taktArticle[0]['t1position3'] < 12  ) {
   $row = "row2";
 }

 if ($taktArticle[0]['t1position3'] > 12 AND $taktArticle[0]['t1position3'] < 17  ) {
   $row = "row3";
 }

 if ($taktArticle[0]['t1position3'] > 17 AND $taktArticle[0]['t1position3'] < 22  ) {
   $row = "row4";
 }

 if ($taktArticle[0]['t1position3'] > 22 AND $taktArticle[0]['t1position3'] < 27  ) {
   $row = "row5";
 }

 // get the columns
 if ($taktArticle[0]['t1position3'] == 3 
     or $taktArticle[0]['t1position3'] == 8 
     or $taktArticle[0]['t1position3'] == 13 
     or $taktArticle[0]['t1position3'] == 18 
     or $taktArticle[0]['t1position3'] == 23) {

   $col = "col1";
 }  

 if ($taktArticle[0]['t1position3'] == 4 
     or $taktArticle[0]['t1position3'] == 9 
     or $taktArticle[0]['t1position3'] == 14 
     or $taktArticle[0]['t1position3'] == 19 
     or $taktArticle[0]['t1position3'] == 24) {

   $col = "col2";
 }

 if ($taktArticle[0]['t1position3'] == 5 
     or $taktArticle[0]['t1position3'] == 10 
     or $taktArticle[0]['t1position3'] == 15 
     or $taktArticle[0]['t1position3'] == 20 
     or $taktArticle[0]['t1position3'] == 25) {

   $col = "col3";
 }

 if ($taktArticle[0]['t1position3'] == 6 
     or $taktArticle[0]['t1position3'] == 11 
     or $taktArticle[0]['t1position3'] == 16 
     or $taktArticle[0]['t1position3'] == 21 
     or $taktArticle[0]['t1position3'] == 26) {

   $col = "col4";
 }

 if ($taktArticle[0]['t1position3'] == 7 
     or $taktArticle[0]['t1position3'] == 12 
     or $taktArticle[0]['t1position3'] == 17 
     or $taktArticle[0]['t1position3'] == 22 
     or $taktArticle[0]['t1position3'] == 27) {

   $col = "col5";
 }

现在......我必须重复一遍($ taktArticle [0] ['t1position3']直到($ taktArticle [0] ['t1position11']

如您所知,代码将变得庞大......任何人都知道如何缩短此代码?

问候,约翰

8 个答案:

答案 0 :(得分:2)

您可以创建清理代码的功能。代码中有明显的模式,寻找这些模式并概括这些模式是清理代码的关键要求。我确信PHP大师可以找到一种更简洁的方法来实现这一点,但一个基本的例子是这样的:

function get_row($position) {
  $row_ranges = array(
    array(3, 7),
    array(7, 12),
    // etc
  );     

  foreach ($row_ranges as $row_index => $range) {
    if ($range[0] < $position && $position < $range[1]) {
       return sprtintf('row%s', $row_index + 1)
    }
  }

}

所有行范围都保存在函数内部的集中位置,并且不再有重复的条件

function get_column($value) {
  // looks like you are starting at 3 and have increments of 5 
  // 3, 8, 13, 18
  // you could loop through and calculate these, or hardcode them in
  //  use `in_array` to clean up the multiple or statements
  if (in_array($value, array(3, 8, 13, 18))) {

  }
}

答案 1 :(得分:1)

我的想法是让一个数组看起来:

$rowtbl = array(4 => 1, 1, 1, 8 => 2, 2, 2, 2);
$row = 'row'.$rowtbl[$taktArticle[0]['t1position3']];

当然数组可能会变大,但你可以从array_merge和range中构建一些东西。

此外,在我看来,你可以做类似的事情:

$row = ceil(($taktArticle[0]['t1position3']-3)/5);
$col = ($taktArticle[0]['t1position3']-3)%5;

你必须检查3和5的确切参数(3个是起点,5个是每行的cols数。

答案 2 :(得分:1)

Christoph已经在第一部分给出了很好的答案。

对于列,而不是IF内的多重比较,请使用in_array

  

现在......我必须重复一遍($ taktArticle [0] ['t1position3']直到($ taktArticle [0] ['t1position11']

选择这种次优数据结构的错误。

为什么这些数据没有组织为$taktArticle[0]['t1position'][3]$taktArticle[0]['t1position'][11],因此您可以轻松地循环这些位置......?

(如果与t1position类似,那么你也有t2positiont3position等等 - 那么那些也应该在数组中组织。)

答案 3 :(得分:1)

您可以使用

if(in_array($taktArticle[0]['t1position3'],array(7,12,17,22,27)))

取代这种陈述

if ($taktArticle[0]['t1position3'] == 7 
OR $taktArticle[0]['t1position3'] == 12 
OR $taktArticle[0]['t1position3'] == 17 
OR $taktArticle[0]['t1position3'] == 22 
OR $taktArticle[0]['t1position3'] == 27)

答案 4 :(得分:1)

在列部分中,您可以检查数组中是否存在OR值,而不是连接多个$taktArticle[0]['t1position3']运算符。

使用PHP in_arrayhttp://php.net/manual/en/function.in-array.php)例如:

if(in_array($taktArticle[0]['t1position3'], [3, 8, 13, 18, 23])) {
    $col = "col1";
}

虽然这更清晰,但您仍然在对此映射中的所有值进行硬编码,因此在添加新案例时,维护开销会增加。

答案 5 :(得分:0)

那里有模式,所以你可以使用除法而不是数组:

$temp = $taktArticle[0]['t1position3']-2
if($temp%5 != 0){
  $row = "row".ceil(($taktArticle[0]['t1position3']-2)/5);
}
$col = "col".(($taktArticle[0]['t1position3']-2)%5);

答案 6 :(得分:0)

或者你可以这样做

   if((((int)$taktArticle[0]['t1position3'])-7)%5==0||((int)$taktArticle[0]['t1position3'])-7)==0)

此声明

   if ($taktArticle[0]['t1position3'] == 7 
   OR $taktArticle[0]['t1position3'] == 12 
   OR $taktArticle[0]['t1position3'] == 17 
   OR $taktArticle[0]['t1position3'] == 22 
   OR $taktArticle[0]['t1position3'] == 27)

答案 7 :(得分:0)

另一种方法是在switch中使用布尔运算符来获取行和递归函数以获得col

//to get Row:
$m = $taktArticle[0]['t1position3'];
switch($m){
    case ($m>3 && $m< 7): $row = 'row1'; break;
    case ($m>7 && $m< 12): $row = 'row2'; break;
    case ($m>12 && $m< 17): $row = 'row3'; break;
    case ($m>17 && $m< 22): $row = 'row4'; break;
    case ($m>22 && $m< 27): $row = 'row5'; break;
}

//To get Col
function rootNum($num){
    return $num-5>0?rootNum($num-5):$num;
}
$n = rootNum($taktArticle[0]['t1position3']);
$col = 'col' . ($n -2);