How to take user input for two dimensional (2D) array in PHP ? Last Updated : 28 Apr, 2023 Comments Improve Suggest changes Like Article Like Report There are two methods to take user input in PHP in two dimensional (2D) array. Approach 1: Use HTML forms through PHP GET & POST method to take user input in two dimensional (2D) array.First, input data into HTML forms.Then use the GET or POST method of PHP to get or post those input data into a variable.Finally, use that variable that holds input data and process using for loop.Though it is from a two-dimensional array so, you need two indices/variables for processing in for loop.Enter input one below another to state as two variables. Example 1: Below example illustrate how to input user data for 2D-array using the Form POST method. php <?php echo "Enter n for nxn : <br>"; echo "<form method='POST'> Row:<input type='number' min='2' max='5' name='1d' value='1'/> Column:<input type='number' min='2' max='5' name='2d' value='1'/> <input type='submit' name='submit' value='Submit'/> </form>"; // Submit user input data for 2D array if (isset($_POST['submit'])) { // POST submitted data $dimention1 = $_POST["1d"]; // POST submitted data $dimention2 = $_POST["2d"]; echo "Entered 2d nxn: " . $dimention1 . "x" . $dimention2 . " <br>"; $d = []; $k = 0; for($row = 0; $row < $dimention1; $row++) { for ($col = 0; $col < $dimention2; $col++) { $d[$row][$col]= $k++; } } for ($row = 0; $row < $dimention1; $row++) { for ($col = 0; $col < $dimention2; $col++) { echo $d[$row][$col]." "; } echo "<br>"; } } ?> Output: Approach 2: To take user input in PHP for two dimensional (2D) by using fopen() function which helps to get user input either at runtime or by the external input file.First, assign those input data to variables.Finally, use those variables which hold input data and process using for loop.Though it is from a two-dimensional array so, you need two indices/variables for processing in for loop. Example: Below example illustrates how to input user data for a 2D array using fopen() function. php <?php // fopen() using standard input $stdin = fopen('php://stdin', 'r'); ?> <?php error_reporting(0); echo "\n\n\nEnter row and column: \n"; // Right trim fgets(user input) $dimention1 = rtrim(fgets($stdin)); // Right trim fgets(user input) $dimention2 = rtrim(fgets($stdin)); echo "Entered row and column: " . $dimention1 . "x" . $dimention1 . " \n"; $d = []; $k = 0; for ($row = 0; $row < $dimention1; $row++) { for ($col = 0; $col < $dimention2; $col++) { $d[$row][$col]= $k++; } } for ($row = 0; $row < $dimention1; $row++) { for ($col = 0; $col < $dimention2; $col++) { echo $d[$row][$col]." "; } echo "\n"; } ?> Output: Reference: https://www.geeksforgeeks.org/php-fopen-function-open-file-or-url/ Comment More infoAdvertise with us Next Article How to take user input for two dimensional (2D) array in PHP ? V VigneshKannan3 Follow Improve Article Tags : Web Technologies PHP PHP-Misc Similar Reads How to declare a Two Dimensional Array of pointers in C? A Two Dimensional array of pointers is an array that has variables of pointer type. This means that the variables stored in the 2D array are such that each variable points to a particular address of some other element. How to create a 2D array of pointers: A 2D array of pointers can be created follo 3 min read How to read user or console input in PHP ? In PHP, the console is a command-line interface, which is also called interactive shell. We can access it by typing the following command in a terminal:Â php -a If we type any PHP code in the shell and hit enter, it is executed directly and displays the output or shows the error messages in case of 4 min read How to get total number of elements used in array in PHP ? In this article, we will discuss how to get total number of elements in PHP from an array. We can get total number of elements in an array by using count() and sizeof() functions. Using count() Function: The count() function is used to get the total number of elements in an array. Syntax: count(arra 2 min read How to find the index of an element in an array using PHP ? In this article, we will discuss how to find the index of an element in an array in PHP. Array indexing starts from 0 to n-1. Here we have some common approachesTable of ContentUsing array_search() FunctionUsing array_flip()Using a custom functionUsing a foreach LoopUsing array_keys FunctionUsing ar 6 min read How to display array structure and values in PHP ? In this article, we will discuss how to display the array structure and values in PHP. To display the array structure and its values, we can use var_dump() and print_r() functions. It includes Array sizeArray valuesArray value with IndexEach value data type We will display the array structure using 2 min read How to Populate Dropdown List with Array Values in PHP? We will create an array, and then populate the array elements to the dropdown list in PHP. It is a common task when you want to provide users with a selection of options. There are three approaches to achieve this, including using a foreach loop, array_map() function, and implode() function. Here, w 2 min read How to convert an array to CSV file in PHP ? To convert an array into a CSV file we can use fputcsv() function. The fputcsv() function is used to format a line as CSV (comma separated values) file and writes it to an open file. The file which has to be read and the fields are sent as parameters to the fputcsv() function and it returns the leng 2 min read How to Loop Through an Array using a foreach Loop in PHP? Given an array (indexed or associative), the task is to loop through the array using foreach loop. The foreach loop iterates through each array element and performs the operations. PHP foreach LoopThe foreach loop iterates over each key/value pair in an array. This loop is mainly useful for iteratin 2 min read How to get form data using POST method in PHP ? PHP provides a way to read raw POST data of an HTML Form using php:// which is used for accessing PHPâs input and output streams. In this article, we will use the mentioned way in three different ways. We will use php://input, which is a read-only PHP stream. We will create a basic HTML form page wh 2 min read How to Insert a New Element in an Array in PHP ? In PHP, an array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index or key.We can insert an element or item in an array using the below function 5 min read Like