15 Jul PHP Arrays
Arrays store multiple values in a variable. For example, to store 10 numbers, you can define an array of length 10, instead of defining 10 variables. PHP arrays include Indexed Arrays, Associative Arrays, and Multi-dimensional Arrays.
In PHP, the array() function is used to create an array. Let’s see an example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<!DOCTYPE html> <html> <body> <?php $cricketers= array("Sachin", "Virat", "Rohit"); echo "Indian Cricketers: " . $cricketers[0] . ", " . $cricketers[1] ." and " . $cricketers[2] . "."; ?> </body> </html> |
Here’s the output,
As shown above, an array is created with the cricketer’s name and then printed with the echo output statement. Here, cricketer names are printed using array indexing i.e. $cricketers[0] for cricketer Sachin, $cricketers[0] for cricketer Virat, and $cricketers[0] for cricketer Rohit,
Now, we will learn about the types of arrays in PHP:
- Indexed (Numeric) Arrays
- Associative Arrays
- Multidimensional Arrays
Indexed (Numeric) Arrays
The Indexed array consists of numeric index, so they are also called Numeric arrays. The array index is a number and the default index starts from 0.
Here’s an example, with an indexed array, topics. The array assigns two elements, which are the names of topics,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<!DOCTYPE html> <html> <body> <?php $topics= array("Java", "Android"); echo "Topics to learn this month: " . $topics[0] . ", " . $topics[1] . "."; ?> </body> </html> |
Here’s the output,
Here, topic names are printed using array indexing i.e. $topics[0] for topic Java, and $topics[1] for topic Android,
Associative Arrays
These are arrays that have named keys associated with them. Here’s an example showing named keys associated with Java and Android,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<html> <body> <?php $topics= array("Java" => 15, "Android" => 20); echo "Days for learning Java: ".$topics['Java'] . "<br />"; echo "Days for learning Android: ".$topics['Android'] . "<br />"; ?> </body> </html> |
Here’s the output,
As shown above, the names keys associated with the array print the number of days for learning Java and Android respectively.
Multidimensional Arrays
An array with one or more than one array is known as a Multidimensional array. Two-dimensional arrays are multi-dimensional arrays and are also considered as an array of arrays.
Here’s an example of two-dimensional arrays, with associative arrays,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<!DOCTYPE html> <html> <body> <?php $points = array( "viratkohli" => array ( "testcricket" => 51, "ODI" => 53, "T20" => 70 ), "joeroot" => array ( "testcricket" => 53, "ODI" => 37, "T20" => 38 ) ); echo "Points for Virat Kohli in ODI Cricket : " ; echo $points['viratkohli']['ODI'] . "<br />"; echo "Points for Joe Root in T20 Cricket : "; echo $points['joeroot']['T20'] . "<br />"; ?> </body> </html> |
Here’s the output,
Here, you can see two-dimensional arrays with names and keys associated with them i.e. associative arrays. It shows the points for two cricketers in all three formats i.e. testcricket, ODI, and T20. Using multi-dimensional arrays, we have shown it for 2 cricketers i.e. two-dimensional arrays.
Here, you can learn about another example. Now, we will print all the values, with an additional array for cricketer David Warner,
Test Cricket | ODI | T20 | |
---|---|---|---|
Virat Kohli | 51 | 53 | 70 |
Joe Root | 53 | 37 | 38 |
David Warner | 50 | 42 | 45 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
<!DOCTYPE html> <html> <body> <?php $points = array( "viratkohli" => array ( "testcricket" => 51, "ODI" => 53, "T20" => 70 ), "joeroot" => array ( "testcricket" => 53, "ODI" => 37, "T20" => 38 ), "davidwarner" => array ( "testcricket" => 50, "ODI" => 42, "T20" => 45 ) ); echo "<h2>Studyopedia Multi-Dimensional Array Example</h2>"; echo "<h3>ODI Cricket</h3>"; echo "Points for Virat Kohli in ODI Cricket : " ; echo $points['viratkohli']['ODI'] . "<br />"; echo "Points for Joe Root in ODI Cricket : "; echo $points['joeroot']['ODI'] . "<br />"; echo "Points for David Warner in ODI Cricket : " ; echo $points['davidwarner']['ODI'] . "<br /><br />"; echo "<h3>T20 Cricket</h3>"; echo "Points for Virat Kohli in T20 Cricket : "; echo $points['viratkohli']['T20'] . "<br />"; echo "Points for Joe Root in T20 Cricket : " ; echo $points['joeroot']['T20'] . "<br />"; echo "Points for David Warner in T20 Cricket : "; echo $points['davidwarner']['T20'] . "<br /><br />"; echo "<h3>Test Cricket</h3>"; echo "Points for Virat Kohli in Test Cricket : " ; echo $points['viratkohli']['testcricket'] . "<br />"; echo "Points for Joe Root in Test Cricket : "; echo $points['joeroot']['testcricket'] . "<br />"; echo "Points for David Warner in Test Cricket : "; echo $points['davidwarner']['testcricket'] . "<br />"; ?> </body> </html> |
No Comments