开发者

How do I write this array in simple method?

I do have a function which do calculations a lot.

$arrleft and $arrright are two different arrays. wha开发者_运维百科t I am doing here is I am combining two to produce together output for my current application. what I done is a simple method and it eats lots of space and time. what I want is if any one can make this code to a couple of lines shorter(I know it can be if it in loop). but cant get into by myself so I am putting it here for your opinion.

  $arrleft = explode(',' , $data1);
$arrright = explode(',' , $data2); if(isset($data1,$data2)){if(isset($arrleft[0],$arrright[0],$arrleft[1],$arrright[1],$arrleft[2],$arrright[2],$arrleft[3],$arrright[3],$arrleft[4],$arrright[4]))
     { $totalnumber = ($valueset+1)//calculate total number of variables set + 1 here $valueset = 4 so $total number (4+1)=5
       $value = (0, 0, $arrleft[0]);
       $value1 = (0, 1, $arrright[0]);
       $value2 = (1, 0, $arrleft[1]);
       $value3 = (1, 1, $arrright[1]);
       $value4 = (2, 0, $arrleft[2]);
       $value5 = (2, 1, $arrright[2]);
       $value6 = (3, 0, $arrleft[3]);
       $value7 = (3, 1, $arrright[3]);
       $value8 = (4, 0, $arrleft[4]);
       $value9 = (4, 1, $arrright[4]);
     }else if(isset($arrleft[0],$arrright[0],$arrleft[1],$arrright[1],$arrleft[2],$arrright[2],$arrleft[3],$arrright[3])){ 
       $totalnumber = ($valueset+1)//calculate total number of variables set + 1 here $valueset = 3 so $total number (3+1)=4
       $value = (0, 0, $arrleft[0]);
       $value1 = (0, 1, $arrright[0]);
       $value2 = (1, 0, $arrleft[1]);
       $value3 = (1, 1, $arrright[1]);
       $value4 = (2, 0, $arrleft[2]);
       $value5 = (2, 1, $arrright[2]);
       $value6 = (3, 0, $arrleft[3]);
       $value7 = (3, 1, $arrright[3]);
     }else if(isset($arrleft[0],$arrright[0],$arrleft[1],$arrright[1],$arrleft[2],$arrright[2])){
        $totalnumber = ($valueset+1)//calculate total number of variables set + 1 here $valueset = 2 so $total number (2+1)=3
       $value = (0, 0, $arrleft[0]);
       $value1 = (0, 1, $arrright[0]);
       $value2 = (1, 0, $arrleft[1]);
       $value3 = (1, 1, $arrright[1]);
       $value4 = (2, 0, $arrleft[2]);
       $value5 = (2, 1, $arrright[2]);
     }else{}
}

I have reduced lots of lines here due to space issue.


Maybe this can help you.

$arrleft = explode(',' , $data1);
$arrright = explode(',' , $data2);
$countleft = count($arrleft);
$countright = count($arrright);

if(isset($data1, $data2) && $countleft == $countright)
{ // i think that you want from these arrays to have same size
  $totalnumber = $countleft; // also $countright
  $values = array(); // you might save these values into array
  $j = 0;

  for($i = 0; $i < $totalnumber; $i++)
  { // i am not sure, but don't you want make from these values an array?
    $values[$j] = array($i, 0, $arrleft[$i]);
    $values[$j + 1] = array($i, 1, $arrright[$i]);
    $j += 2;
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜