w3resource

PHP Array Exercises : Create a two-dimensional array

PHP Array: Exercise-56 with Solution

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:

Flowchart: PHP - Create a two dimensional array

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/php-exercises/php-array-exercise-56.php