Bifalgorithm post · Computer science one · Multidimensional Arrays

How can two indices be used to set values in a two-dimensional PHP array?

How can two indices be used to set values in a two-dimensional PHP array? (Computer science one · Multidimensional Arrays) — "Alternatively, you can use two in…

Ideas in this flowchart

  • Using two indices with a two-dimensional array
  • The array is accessed and assigned with $mat[$i][$j].
  • The outer loop uses $i from 0 through 2.
  • The inner loop uses $j from 0 through 3.
  • Nested loops process each row and column position.
  • Each element receives the value ($i + $j) * 3.
  • The resulting array contains three rows and four columns.
  • Row 0 contains 0, 3, 6, and 9.
  • Row 1 contains 3, 6, 9, and 12.
  • Row 2 contains 6, 9, 12, and 15.
Feed/

How can two indices be used to set values in a two-dimensional PHP array?

No cover

Channel

Computer science one

1 page

"Alternatively, you can use two indices to get and set values from a 2-dimensional array.\n\nfor($i=0; $i<3; $i++) {\n\nfor($j=0; $j<4; $j++) {\n\n$mat[$i][$j] = ($i+$j)*3;\n\n}\n\n}\n\nwhich results in:\n\nArray (\n\n[0] => Array ( [0] => 0 [1] => 3 [2] => 6 [3] => 9 )\n\n[1] => Array ( [0] => 3 [1] => 6 [2] => 9 [3] => 12 )\n\n[2] => Array ( [0] => 6 [1] => 9 [2] => 12 [3] => 15 )\n\n)"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.