Lecture 9 - PHP - Arrays
Lecture 9 - PHP - Arrays
Arrays
• An array is a data structure that stores one or more similar type of
values in a single value. For example if you want to store 100 numbers
then instead of defining 100 variables its easy to define an array of 100
length.
• There are three different kind of arrays and each array value is accessed
using an ID c which is called array index.
• Numeric array − An array with a numeric index. Values are stored and
accessed in linear fashion.
• Associative array − An array with strings as index. This stores element
values in association with key values rather than in a strict linear index
order.
• Multidimensional array − An array containing one or more arrays and
values are accessed using multiple indices
Numeric Array
• These arrays can store numbers, strings and any
object but their index will be represented by
numbers. By default array index starts from zero.
• Example
• Following is the example showing how to create
and access numeric arrays.
• Here we have used array() function to create
array.
Numeric Array
• <html>
• <body>
•
• <?php
• /* First method to create array. */
• $numbers = array( 1, 2, 3, 4, 5);
•
• foreach( $numbers as $value ) {
• echo "Value is $value <br />";
• }
•
Numeric Array
• /* Second method to create array. */
• $numbers[0] = "one";
• $numbers[1] = "two";
• $numbers[2] = "three";
• $numbers[3] = "four";
• $numbers[4] = "five";
•
• foreach( $numbers as $value ) {
• echo "Value is $value <br />";
• }
• ?>
•
• </body>
• </html>
Numeric Array
• This will produce the following result −
• Value is 1
• Value is 2
• Value is 3
• Value is 4
• Value is 5
• Value is one
• Value is two
• Value is three
• Value is four
• Value is five
Associative Arrays
• The associative arrays are very similar to numeric
arrays in term of functionality but they are different
in terms of their index. Associative array will have
their index as string so that you can establish a
strong association between key and values.
• To store the salaries of employees in an array, a
numerically indexed array would not be the best
choice. Instead, we could use the employees names
as the keys in our associative array, and the value
would be their respective salary
Associative Arrays
• <html>
• <body>
•
• <?php
• /* First method to associate create array. */
• $salaries = array("mohammad" => 2000, "qadir" => 1000, "zara"
=> 500);
•
• echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
• echo "Salary of qadir is ". $salaries['qadir']. "<br />";
• echo "Salary of zara is ". $salaries['zara']. "<br />";
•
Associative Arrays
• /* Second method to create array. */
• $salaries['mohammad'] = "high";
• $salaries['qadir'] = "medium";
• $salaries['zara'] = "low";
•
• echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
• echo "Salary of qadir is ". $salaries['qadir']. "<br />";
• echo "Salary of zara is ". $salaries['zara']. "<br />";
• ?>
•
• </body>
• </html>
Associative Arrays
• This will produce the following result −