1

I have two arrays,

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

my program is:

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

Question is how can I eliminate the inner loop or the j loop?

Since the $col array already know which index to pick, how can I format a variable of a variable so that j loop can be replace by

$result = $data[1st element in $col].","$data[2nd element in $col].","...;
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
KQL
  • 15
  • 7

1 Answers1

0

it's have different meaning of ++$i and $i++,

change the ++i and ++j to $i++ and $j++

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

reference : What is the difference between ++i and i++?

Community
  • 1
  • 1
Josua Marcel C
  • 3,122
  • 6
  • 45
  • 87