2. arrays
Arrays in PHP is a type of data structure that allows us to store multiple elements of
similar data type under a single variable
The arrays are helpful to create a list of elements of similar types, which can be
accessed using their index or key
There are three different kind of arrays and each array value is accessed using an ID
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
3. Indexed or Numeric Arrays
These type of arrays can be used to store any type of elements, but an index
is always a number.
By default, the index starts at zero.
There are two ways to create indexed arrays:
The index can be assigned automatically (index always starts at 0), like this:
$cars = array("Volvo", "BMW", "Toyota");
or the index can be assigned manually:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
4. <html>
<body>
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
</body>
</html>
Output:
I like Volvo, BMW and Toyota.
5. <?php
// One way to create an indexed array
$name_one = array("Zack", "Anthony", "Ram", "Salim", "Raghav");
// Accessing the elements directly
echo "Accessing the 1st array elements directly:n";
echo "<br>";
echo $name_one[2], "n";
echo $name_one[0], "n";
echo $name_one[4], "n";
echo "<br>";
// Second way to create an indexed array
$name_two[0] = "ZACK";
$name_two[1] = "ANTHONY";
$name_two[2] = "RAM";
$name_two[3] = "SALIM";
$name_two[4] = "RAGHAV";
echo "<br>";
// Accessing the elements directly
echo "Accessing the 2nd array elements directly:n";
echo "<br>";
echo $name_two[2], "n";
echo $name_two[0], "n";
echo $name_two[4], "n";
?>
Output
Accessing the 1st array elements directly:
Ram Zack Raghav
Accessing the 2nd array elements directly:
RAM ZACK RAGHAV
6. 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.
7. <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 />";
/* 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>
Output:
Salary of mohammad is 2000
Salary of qadir is 1000
Salary of zara is 500
Salary of mohammad is high
Salary of qadir is medium
Salary of zara is low
8. Multidimensional Arrays
Multi-dimensional arrays are such arrays that store another array at each
index instead of a single element.
multi-dimensional arrays as an array of arrays.
As the name suggests, every element in this array can be an array and they
can also hold other sub-arrays within
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
9. <html>
<body>
<?php
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";
echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br>";
?>
</body>
</html>
Output:
Volvo: In stock: 22, sold: 18.
BMW: In stock: 15, sold: 13.
Saab: In stock: 5, sold: 2.
Land Rover: In stock: 17, sold: 15.
10. PHP foreach loop
The foreach loop is used to traverse the array elements.
It works only on array and object.
The foreach loop works on elements basis rather than index.
It provides an easiest way to iterate the elements of an array.
Syntax
foreach ($array as $value) {
//code to be executed
}
11. <html>
<body>
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
</body>
</html>
Output:
red
green
blue
yellow