PHP Multidimensional Array

An array of arrays is called multidimensional array. It is used to store the data in a tabular form. It is represented in the form of matrix (row*column). Example: We can store the data in the form of two dimensional arrays.
Emp-id Name Salary
John 20 30000
Helena 30 60000
Rasmus 29 40000
 
<!DOCTYPE html>
<html>
<head>
<title>PHP Tutorial</title>
</head>
<body>
<?php
$emp = array
(
array(1,"john",300000),
array(2,"Helena",60000),
array(3,"Rasmus",40000)
);
//traversal 2D elements of an array in the form of (row*column)
for ($row = 0; $row < 3; $row++) {
for ($col = 0; $col < 3; $col++) {
echo $emp[$row][$col]."  ";
}
echo "<br/>";
}
?>
</body>
</html>
 Output 1 john 300000 2 Helena 60000 3 Rasmus 40000 Example2
<!DOCTYPE html>
<html>
<head>
<title>PHP Tutorial</title>
</head>
<body>
<?php
error_reporting(1);
$emp=array(array("emp_id"=>1,"name"=>"John","mob"=>9143434323),
array("emp_id"=>2,"name"=>"Helena","mob"=>9134433235),
array("emp_id"=>3,"name"=>"Rasmus","mob"=>9133534323)
);
echo '<table border="3">';
echo '<tr>';
echo '<td align="center">Employee Id</td>';
echo '<td align="center">Employee Name</td>';
echo '<td align="center">Mobile no</td>';
foreach($emp as $k){
echo '<tr>';
foreach($k as $v){
echo '<td align="center">'.$v.'</td>';
}
echo '</tr>';
}
echo '</table>';
?>
</body>
</html>
Output Example 3
<!DOCTYPE html>
<html>
<head>
<title>PHP Tutorial</title>
</head>
<body>
<form method="get">
Select Country:<select name="c">
<option value="ind">india</option>
<option value="pak">Pakistan</option>
<option value="ch">Australia</option>
</select>
<input type="submit" value="submit" name="display"/>
</form>
<?php
$country=array(
"ind"=>array("Andhra Pradesh","Arunachal Pradesh","Assam","Bihar","Rajastha"),
"pak"=>array("Islamabad","Lahore"),
"ch"=>array("Victoria","Queensland","SouthAustralia","WesternAustralia","New South Wales")
);
if(isset($_GET['display'])){
$get_country=$_GET['c'];
echo "<br>"."Select City ";
foreach($country as $country_key => $cname){
if($country_key==$get_country){
echo "<select>";
foreach($cname as $state){
echo "<option>".$state."</option>";
}
echo "</select>";
}
}
}
?>
</body>
</html>
Output

Related Topics

PHP trim() Function

PHP trim() Function The trim() Function in PHP strips the whitespace or other characters from the beginning and the end of the given string. The trim() function strips the following characters: " " - an ordinary space."\t"- a tab."\n"- a new...

1 minute read.

PHP array_reduce() Function

PHP array_reduce() Function The array_reduce() function in PHP reduces the array to a single value using by applying a callback function to the elements of the array. Syntax array_reduce ( array $array , callable $callback [, mixed $initial = NULL ] ) Parameter array(required)- This parameter represents the input array. callback (required)- This...

1 minute read.

Multiple-Inheritance in PHP

Multiple-Inheritance in PHP Multiple-Inheritance is the resources of the Object Oriented Programming Languages in which child class or subclass can inherit the resources of the multiple parent classes or superclasses. PHP does...

3 minutes read.

PHP array_key_first() function

The array_key_first () function in PHP gets the first key of an array if the specified array is not empty. Syntax array_key_first(array;$array) Parameter array(required)- This parameter signifies the input array. Return This function returns the first key of the specified...

1 minute read.

PHP strrchr() Function

PHP strrchr() Function The strrchr() function in PHP finds the last occurrence of a character in the specified string and returns the portion of haystack, which starts at the last occurrence of the needle and goes until the end of haystack....

1 minute read.

PHP str_getcsv() Function

PHP str_getcsv() Function The str_getcsv() function in PHP parses a CSV string into an array and returns an array containing the fields read. Syntax str_getcsv ( string $input [, string $delimiter [, string $enclosure [, string$escape ]]] ) Parameter input(required)- This parameter represents the string to parse. delimiter(optional)- This parameter signifies...

1 minute read.

PHP key() Function

PHP key() Function The key() function in PHP fetches a key from an array and returns the index element of the current array position. Syntax key ( array $array ) Parameter array(required)- This parameter represents the input array. Return This function returns the key of the...

1 minute read.

PHP Continue

What is continue in PHP? Inside the loop, the continue statement is used to control the loop in preprocessor hypertext. PHP utilizes the continue keyword to implement the continue statement, which...

3 minutes read.

PHP get_defined_vars() Function

PHP get_defined_vars() Function The get_defined_vars() function in PHP returns an array of all defined variables. It returns a multidimensional array containing a list of all defined variables. Syntax get_defined_vars ( void ) Parameter NA Return This function returns a multidimensional array containing a list...

1 minute read.

PHP metaphone() Function

PHP metaphone() Function The metaphone() function in PHP calculates the metaphone key (representing how a string sounds or pronounced) of a string. This function is used to text search and text matching or spelling...

1 minute read.

PHP wordwrap() Function

PHP wordwrap() Function The wordwrap() Function wraps a string to a given number of characters using a string break character. Syntax wordwrap ( string $str [, int $width [, string $break  [, bool $cut  ]]] ) Parameter str(required)- This parameter specifies the input string.       width(optional)- This parameter represents the number of...

1 minute read.

PHP number_format() Function

PHP number_format() Function The number_format() function in PHP formats a number with grouped thousands. This function accepts one, two, or four parameters (not three): Syntax number_format ( float $number [, int $decimals ] )             or number_format ( float $number , int $decimals  , string $dec_point, string$thousands_sep ) Parameter number(required)- This parameter represents the number to be formatted. If...

1 minute read.

PHP For loop with Example

For loop in PHP : The for loop is the most potent loop, it combines the abilities to set up variables as we enter the loop, test for conditions while iterating...

3 minutes read.

PHP strip_tags() Function

PHP strip_tags() Function The strip_tags() function in PHP is sued to strip a string from HTML, XML, and PHP tags. It is a binary-safe function. Syntax strip_tags ( string $str [, string $allowable_tags ] ) Parameter str(required)- This parameter specifies the input string. allowable_tags(optional)- This parameter represents...

1 minute read.

PHP parse_str() Function

PHP parse_str() Function The parse_str() function in PHP parses a query string into variables. Syntax parse_str ( string $encoded_string [, array &$result ] ) Parameter encoded_string(required)- This parameter represents the input string. result(optional)- This parameter specifies the name of an...

1 minute read.

Python Set pop() method

Python Set pop() method The set.pop() method in Python removes an arbitrary element (random item) from the set and returns the element removed. Syntax set.pop()  Parameter NA Return This method returns an arbitrary (random) element from the set. Example 1 #...

1 minute read.

PHP array_combine() Function

PHP array_combine() Function The array_combine() function in PHP is used to create an array by combining two arrays i.e., one array for keys and another array for its values. Syntax array_combine ( array $keys , array $values ) Parameter keys(required)- This parameter represents an array which acts...

1 minute read.

PHP is_iterable() Function

The is_iterable() function in PHP verifies that the contents of the specified variable is an iterable value. Syntax is_iterable ( mixed $var ) var(required)- This parameter represents the value to check. Return This function returns a Boolean value TRUE if the parameter var is iterable, else it returns...

1 minute read.

PHP array_replace() Function

PHP array_replace() Function The array_replace() function in PHP replaces the elements from passed arrays into the first array with values having the same keys in each of the following arrays. Syntax array_replace ( array $array1 [, array $... ] ) Parameter array(required)- This parameter represents...

1 minute read.

PHP end() function

PHP end() function The end() function in PHP sets the internal pointer of an array to its last element. This function returns the value of the last element Syntax end ( array &$array )   Parameter array (required)– This parameter represents the input array. Return This function returns...

1 minute read.