php以编程方式选择字段而不循环

时间:2015-07-03 05:56:04

标签: php variables

我有两个数组,

$col = array(1,3,5,7,10...)
 $data = array(1000 by 1000)

我的节目是:

$result = "";
for($i=0; $i<count($data); ++i){
    for($j=0; $j<count($col); ++j){
         $result .= ",".$data[$j]
    }
$result .= "<br>";
}
echo $result;
  

问题是我如何eliminate inner loopj loop

由于$col数组已经知道要选择哪个索引,我如何格式化变量的变量以便j循环可以替换为

$result = $data[1st element in $col].","$data[2nd element in $col].","...;

1 个答案:

答案 0 :(得分:0)

它具有++$i$i++

的不同含义

将++ i和++ j更改为$ i ++和$ j ++

$result = "";
for($i=0; $i<count($data); $i++){
    for($j=0; $j<count($col); $j++){
         $result .= ",".$data[$j]
    }
$result .= "<br>";
}
echo $result;

参考:What is the difference between ++i and i++?