0% found this document useful (0 votes)
5 views31 pages

12. Array Functions

The document provides an overview of various PHP array functions including count(), list(), in_array(), current(), next(), prev(), end(), sort(), rsort(), asort(), arsort(), array_merge(), and array_reverse(). Each function is explained with its syntax, parameters, and examples demonstrating its usage. These functions are essential for manipulating and managing arrays in PHP.

Uploaded by

agauubaal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views31 pages

12. Array Functions

The document provides an overview of various PHP array functions including count(), list(), in_array(), current(), next(), prev(), end(), sort(), rsort(), asort(), arsort(), array_merge(), and array_reverse(). Each function is explained with its syntax, parameters, and examples demonstrating its usage. These functions are essential for manipulating and managing arrays in PHP.

Uploaded by

agauubaal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Advanced Web Development Using

count() Function
• The count() function returns the number of
elements in an array.
– Syntax
• count(array,mode);
Count() Parameters

Parameter Description
array Required. Specifies the array
mode •Optional. Specifies the mode. Possible values:0 - Default. Does not
count all elements of multidimensional arrays
•1 - Counts the array recursively (counts all the elements of
multidimensional arrays)
Normal Count() Example
• <?php
$cars=array("Volvo","BMW","Toyota");
echo count($cars);
?>
Recursive Count() Example
• <?php
$cars=array("Volvo"=>array("XC60","XC90"),
"BMW"=>array("X3","X5"),
"Toyota"=>array("Highlander"));

echo "Normal count: " . count($cars)."<br>";


echo "Recursive count: " . count($cars,1);
?>
list() Function
• The list() function is used to assign values to a
list of variables in one operation.
– Syntax
• list(var1,var2...)
List() Example
• <?php
$my_array = array("Dog","Cat","Horse");

list($a, $b, $c) = $my_array;


echo "I have several animals, a $a, a $b and a
$c.";
?>
List() Example
• <?php
$my_array = array("Dog","Cat","Horse");

list($a, , $c) = $my_array;


echo "Here I only use the $a and $c
variables.";
?>
in_array() Function
• The in_array() function searches an array for a
specific value.
– Syntax
• in_array(search,array)
In_array() Example
• <?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");

if (in_array("Glenn", $people))
{
echo "Match found";
}
else
{
echo "Match not found";
}
?>
current() Function
• The current() function returns the value of the
current element in an array.
• Every array has an internal pointer to its
"current" element, which is initialized to the
first element inserted into the array.
Tip: This function does not move the arrays
internal pointer.
Current() Example
• <?php
$people = array("Peter", "Joe", "Glenn",
"Cleveland");

echo current($people) . "<br>";


?>
next() Function
• The next() function moves the internal pointer
to, and outputs, the next element in the array.
Next() Example
• <?php
$people = array("Peter", "Joe", "Glenn",
"Cleveland");

echo current($people) . "<br>";


echo next($people);
?>
prev() Function
• The prev() function moves the internal pointer
to, and outputs, the previous element in the
array.
prev() Example
• <?php
$people = array("Peter", "Joe", "Glenn",
"Cleveland");

echo current($people) . "<br>";


echo next($people) . "<br>";
echo prev($people);
?>
end() Function
• The end() function moves the internal pointer
to, and outputs, the last element in the array.
End() Example
• <?php
$people = array("Peter", "Joe", "Glenn",
"Cleveland");

echo current($people) . "<br>";


echo end($people);
?>
sort() Function
• The sort() function sorts an indexed array in
ascending order.
– Syntax
• sort(array,sortingtype);
Sort() Example

Parameter Description
array Required. Specifies the array to sort
sortingtype •Optional. Specifies how to compare the array elements/items. Possible
values:0 = SORT_REGULAR - Default. Compare items normally (don't
change types)
•1 = SORT_NUMERIC - Compare items numerically
•2 = SORT_STRING - Compare items as strings
Sort() Example
• <?php
$numbers=array(4,6,2,22,11);
sort($numbers);
foreach ($numbers as $i)
{
echo $i." ";
}

?>
rsort() Function
• The rsort() function sorts an indexed array in
descending order.
– Syntax
• rsort(array,sortingtype);
Rsort() Example
• <?php
$cars=array("Volvo","BMW","Toyota");
rsort($cars);
?>
asort() Function
• The asort() function sorts an associative array
in ascending order, according to the value.
– Syntax
• asort(array,sortingtype);
asort() Example
• <?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=
>"43");
asort($age);
?>
arsort() Function
• The arsort() function sorts an associative array
in descending order, according to the value.
– Syntax
• arsort(array,sortingtype);
arsort() Example
• <?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=
>"43");
arsort($age);
?>
array_merge() Function
• The array_merge() function merges one or
more arrays into one array.
– Syntax
• array_merge(array1,array2,array3...)
array_merge() example
• <!DOCTYPE html>
<html>
<body>

<?php
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));
?>

</body>
</html>
• Output :
– Array ( [0] => red [1] => green [2] => blue [3] => yellow )
array_reverse() Function
• The array_reverse() function returns an array
in the reverse order.
– Syntax
• array_reverse(array)
array_reverse() Function
• <!DOCTYPE html>
<html>
<body>

<?php
$a=array("a"=>"Volvo","b"=>"BMW","c"=>"Toyot
a");
print_r(array_reverse($a));
?>

</body>
</html>
Output:
Array ( [c] => Toyota [b] => BMW [a] => Volvo )

You might also like