Laravel
258
w3alert.com
08-01-2020
An array is a type of special variable in PHP. It stores multiple elements/items of the same data type in a single variable. So that does not need to declare any other variable for each data. With this, it creates a list of items/elements of a similar data type that can be accessed using its index/key.
Note: You can define array in PHP by using PHP array function " array( ); " and square bracket " [ ] ".
Example: The following Example of An array in PHP.
$tutorial = array( "PHP", "PYTHON", "NODE JS");
OR
$tutorial = [ "PHP", "PYTHON", "NODE JS" ];
<!DOCTYPE html> <html> <body> <?php $tutorial = [ "PHP", "PYTHON", "NODE JS" ]; // Print array Print_r($tutorial); echo "<br>"; //Access the array value by key echo $tutorial[0] ."<br>"; echo $tutorial[1] ."<br>"; echo $tutorial[2] ."<br>"; ?> </body> </html>
According to Example, " $tutorial " it is a variable In which "PHP", "PYTHON", "NODE JS" elements are stored.
1. | Key | Defines the key Numeric / String. |
2. | Value | Defines the value. |
PHP Indexed Array is used to hold any type of item/element. In Array, index/key will be represented by a number. By default, PHP indexed array begins with 0. it is also known as a numeric array.
array(value1, value2, value3);
Examples: The following Examples for understanding PHP Indexed Arrays.
<!DOCTYPE html> <html> <body> <?php $source = array("welcome","to","w3alert.com"); echo $source[0] ." ". $source[1] ." ". $source[2] . "."; // keys are 0,1,2 ?> </body> </html>
<!DOCTYPE html> <html> <body> <?php $books = array( "Wings of Fire","The Secret","War and Peace"); $arrlen=count($books); // count the array length for( $m = 0; $m < $arrlen; $m++ ) { echo $books[$m]; echo "<br>"; } ?> </body> </html>
The associative array is similar to the indexed array. Associative arrays are held elements or values in key-value pairs.
The array whose keys reside in string form is called array association array.
array("key1"=>"value1", "key2"=>"value2", "key"=>"value3");
Examples: The following Examples for understanding PHP Associative Arrays.
<!DOCTYPE html> <html> <body> <?php $laptop_price = array( "Dell Vostro" => "25995","HP Notebook" => "21290","Lenovo Ideapad" => "27990" ); echo "The Price of Dell Vostro:"." ". $laptop_price['Dell Vostro'] ."<br>"; echo "The Price of HP Notebook:"." ". $laptop_price['HP Notebook'] ."<br>"; echo "The Price of Lenovo Ideapad:"." ". $laptop_price['Dell Vostro'] ."<br>"; ?> </body> </html>
<!DOCTYPE html> <html> <body> <?php $old_year = array("Gretel"=>"20","Elizabeth"=>"24"); $keys = array_keys($old_year); $arrlength = count($old_year); for($l=0; $l < $arrlength; $l++) { echo $keys[$l] . ' ' . $old_year[$keys[$l]] . "\n"; } ?> </body> </html>
<!DOCTYPE html> <html> <body> <?php $laptop_price = array( "Dell Vostro" => "25995","HP Notebook" => "21290","Lenovo ideapad" => "27990" ); foreach($laptop_price as $m => $m_value) { echo "Key=" . $m . ", Value=" . $m_value; echo "<br>"; } ?> </body> </html>
PHP Multidimensional array is used to store more than one arrays. Each element in the array holds the other sub-arrays. PHP Multidimensional array has multiple indexes to access the sub-array.
array ( array ( value1, value2, value3 ), array ( value1, value2, value3 ), );
Examples: The following Examples for understanding PHP Multidimensional Arrays.
<!DOCTYPE html> <html> <body> <?php // create PHP Multidimensional Array $info = array ( array("name"=>"John","age"=>"23","specialist"=>"PHP"), array("name"=>"Marco","age"=>"28","specialist"=>"PYTHON"), array("name"=>"Devin","age"=>"32","specialist"=>"PHP,JAVA,PYTHON"), ); // for Sub-array print_r($info[0]); echo "<br>"; print_r($info[1]); echo "<br>"; print_r($info[2]); echo "<br>"; // for Single Value print_r($info[0]["name"]); echo "<br>"; print_r($info[1]["name"]); echo "<br>"; print_r($info[2]["name"]); echo "<br>"; ?> </body> </html>
<!DOCTYPE html> <html> <body> <?php $info = array ( array("name"=>"John","age"=>"23","specialist"=>"PHP"), array("name"=>"Marco","age"=>"28","specialist"=>"PYTHON"), array("name"=>"Devin","age"=>"32","specialist"=>"PHP,JAVA,PYTHON"), ); foreach($info as $key => $value){ echo "<p><b> List $key Item</b></p>"; echo "<ul>"; foreach($value as $key1 => $value){ echo "<li>" .$key1. " = " .$value. "</li>"; } echo "</ul>"; } ?> </body> </html>