1

我有以下 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']

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

问候,约翰

4

8 回答 8

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))) {

  }
}
于 2013-09-24T15:04:37.763 回答
1

Good answer on the first part by Christoph already.

As for the columns, instead of multiple comparisons inside the IFs, use in_array.

Now... I have to repeat this from ($taktArticle[0]['t1position3'] until ($taktArticle[0]['t1position11']

Your fault for choosing such a sub-optimal data structure.

Why is this data not organized as $taktArticle[0]['t1position'][3] to $taktArticle[0]['t1position'][11], so that you could easily loop over the positions …?

(And if there’s analogues to t1position, so you have t2position, t3position etc. as well – then those should be organized in arrays too.)

于 2013-09-24T15:07:02.413 回答
1

You can use

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

In place of this type of statement

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)
于 2013-09-24T15:07:46.147 回答
1

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

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

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

虽然这样更简洁,但您仍然在此映射中对所有值进行硬编码,因此维护开销会随着您添加新案例而增加。

于 2013-09-24T15:15:38.963 回答
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 是每行的列数。

于 2013-09-24T15:03:43.670 回答
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);
于 2013-09-24T15:34:08.873 回答
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);
于 2013-09-24T15:13:59.443 回答
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)
于 2013-09-24T15:16:30.633 回答