PHP Array Exercises : Create a two-dimensional array
Write a PHP script to create a two-dimensional array (4x4), initialized to 10.
Note: Use array_fill() function.
Sample Solution:
PHP Code:
<?php
// Create a multidimensional array filled with the value 10
// The outer array has 4 elements, and each element is an inner array
// The inner arrays have 4 elements each, all initialized with the value 10
$a = array_fill(0, 4, array_fill(0, 4, 10));
// Print the resulting multidimensional array
print_r($a);
?>
Output:
Array ( [0] => Array ( [0] => 10 [1] => 10 [2] => 10 [3] => 10 ) [1] => Array ( [0] => 10 [1] => 10 [2] => 10 [3] => 10 ) [2] => Array ( [0] => 10 [1] => 10 [2] => 10 [3] => 10 ) [3] => Array ( [0] => 10 [1] => 10 [2] => 10 [3] => 10 ) )
Flowchart:
PHP Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a PHP function to convert a string to an array (trimming every line and remove empty lines).
Next: Write a PHP function to compares two multidimensional arrays and returns the difference.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics