In PHP language, there are different types of arrays like:
We use them according to our requirements.
PHP array provides the index number to each of the array elements. These index numbers are used to perform different operations on arrays.
For example:
Index
The index is the unique number provided to each of the array elements in ascending order starting from zero.
Suppose we have an indexed array of different shapes as follows:
$shapes = array(“circle”,”rectangle”,”square”,”triangle”);
In the above-indexed array, PHP provides index numbers as follows:
Array element index number
circle – 0
rectangle – 1
square – 2
triangle – 3
We can use these index numbers to print the elements of an array.
Suppose we have an array of different shapes, and we want to print an array’s values.
<!DOCTYPE html>
<html>
<body>
<?php
$shapes = array("circle","rectangle","square","triangle");
echo $shapes[0]."<br>";
echo $shapes[1]."<br>";
echo $shapes[2]."<br>";
echo $shapes[3]."<br>";
?>
</body>
</html> Output
circle
rectangle
square
triangle
Explanation: In the above example, we print the array’s values using array index no.
We can also use the foreach method with index numbers to print the array as follows:
<!DOCTYPE html>
<html>
<body>
<?php
$shapes = array("circle","rectangle","square","triangle");
foreach($shapes as $value){
echo $value."<br>";
}
?>
</body>
</html> Output
circle
rectangle
square
triangle
Explanation: In the above code, we get the output as same as example 2. Here we use the index number to print the index array.
The PHP index number is also used to check the position of the array element, and based on its position, and we can take different actions as follows:
Suppose we have an array of shapes, and we want to print the index number of the array element with its value at the first and last position. We can check the condition as follows:
<!DOCTYPE html>
<html>
<body>
<?php
$shapes = array("circle","rectangle","square","triangle");
$counter = 0;
foreach($shapes as $value){
// check for first position
if($counter == 0) {
echo "First Index:<br>";
echo "Index number: ".$counter. " value: ".$value."<br>";
}
// check for last position
if($counter == count($shapes) - 1){
echo "Last Index:<br>";
echo "Index number: ".$counter. " value: ".$value."<br>";
}
$counter +=1;
}
?>
</body>
</html> Output
First Index:
Index number: 0 value: circle
Last Index:
Index number: 3 value: triangle
Explanation
We can also use the index number in an associative array containing keys and values. In this array, we can also check conditions based on the resulting echo of some statements.
Suppose we have the following array :
$arr = array(“circle”=>”blue”,”rectangle”=>”red”,”square”=>”black”,”triangle”=>”pink”);
and we want to echo the key and value at first and the last position we can do this as follows:
<!DOCTYPE html>
<html>
<body>
<?php
$shapes = array("circle"=>"blue","rectangle"=>"red","square"=>"red","triangle"=>"pink");
$counter = 0;
foreach($shapes as $key => $value){
if($counter == 0){
// check for first position
echo "First Index:<br>";
echo "Key = ".$key." and value = ".$value."<br>";
}
if($counter == count($shapes) -1){
// check for last position
echo "Last Index:<br>";
echo "Key = ".$key." and value = ".$value."<br>";
}
$counter+=1;
}
?>
</body>
</html> Output
First Index:
Key = circle and value = blue
Last Index:
Key = triangle and value = pink
Explanation: Here, we also use the same counter variable logic in the above associative array.
This is all about the index of the foreach method of PHP. In an indexed array, we can use the foreach loop index for printing the values of an array. We can also check some conditions for array elements, and based on the result, further actions are taken in PHP code.
I hope you found this post fully informative and helpful.
Thank you for reading 🙂
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…