Foreachloopinarraypptx 2024 10 17 19 09 07
Foreachloopinarraypptx 2024 10 17 19 09 07
AnD
PHP foreach loop
built-in functions
1. array()– Create an Array
The array() function is used to create an array in PHP.
2. count()– Count the Elements in an Array
The count() function returns the number of elements in an array.
3. array_merge()– Merge Two or More Arrays
The array_merge() function merges the elements of one or more arrays.
4. array_push()– Add One or More Elements to the End of an Array
The array_push() function adds one or more elements to the end of an array.
5. array_pop()– Remove the Last Element of an Array
The array_pop() function removes the last element from an array.
6. array_shift() – Remove the First Element of an Array
The array_shift() function removes the first element from an array.
7. array_unshift() – Add One or More Elements to the Beginning of an Array
The array_unshift() function adds one or more elements to the beginning of an
array.
8. array_key_exists() – Check if a Key Exists in an Array
The array_key_exists() function checks if a specific key exists in an associative
array.
9. in_array() – Check if a Value Exists in an Array
The in_array() function checks if a value exists in an array.
10. array_keys() – Return All the Keys of an Array
The array_keys() function returns all the keys from an array.
11. array_values() – Return All the Values of an Array
The array_values() function returns all the values from an array.
12. array_unique() – Remove Duplicate Values from an Array
The array_unique() function removes duplicate values from an array.
13. sort() – Sort an Array in Ascending Order
The sort() function sorts the elements of an array in ascending order.
14. rsort() – Sort an Array in Descending Order
The rsort() function sorts the elements of an array in descending order.
15. array_combine() – Combine Two Arrays (Keys and Values)
The array_combine() function creates an associative array by using one
array for keys and another for values.
PHP foreach loop
The foreach loop is used to traverse the array elements. It works only on
array and object. It will issue an error if you try to use it with the
variables of different datatype.
The foreach loop works on elements basis rather than index. It provides
an easiest way to iterate the elements of an array.
In foreach loop, we don't need to increment the value.
Syntax
<?php
$season = array ("Summer", "Winter", "Autumn", "Rainy");
foreach ($season as $element) {
echo "$element";
echo "</br>";
}
?>
Example 2:
PHP program to print associative array elements using
foreach loop.
<?php
$employee = array (
"Name" => "Alex",
"Email" => "[email protected]",
"Age" => 21,
"Gender" => "Male"
);
foreach ($employee as $key => $element) {
echo $key . " : " . $element;
echo "</br>";
}
?>
Example 3: Multi-dimensional array
<?php
$a = array();
$a[0][0] = "Alex";
$a[0][1] = "Bob";
$a[1][0] = "Camila";
$a[1][1] = "Denial";
foreach ($a as $e1) {
foreach ($e1 as $e2) {
echo "$e2\n";
}
}
?>
PHP code to find the maximum and minimum elements of an array.
<?php $array =
function getMaxElement($array) array(10,20,30,40,50);
{
$n = count($array); echo
$max = $array[0]; (getMaxElement($array)
for ($i = 1;$i < $n;$i++) if ($max < $array[$i]) $max = $array[$i]; );
return $max; echo ("\n");
}
function getMinElement($array) echo
{ (getMinElement($array))
$n = count($array); ;
$min = $array[0];
?>
for ($i = 1;$i < $n;$i++) if ($min > $array[$i]) $min = $array[$i];
return $min;
}