samedi 29 février 2020

how to make following pattern in php using nested for loop

I want to make matrix 512 columns x 256 rows, with the pattern like this as:

expected result

I was trying it with this one:

$b = 0;
matrik = [[]];
  for ($i=0; $i < 512; $i++) { 
     for ($j=0; $j < 256; $j++) { 
         if ($j=$b) {
             $matrik[$j][$j] = 1;
         }else{
             $matrik[$j][$j] = 0;
         }
         $b++;
      }
  }
return $matrik;

but output of this code was:

[
 [
   0
 ],
 {
   1: 1
 },
 {
   2: 1
 },
 {
   3: 1
 },
 {
   4: 1
 },
 {
   5: 1
 },

Aucun commentaire:

Enregistrer un commentaire