Begin Web Programming with PHP & MySQL

Advertisements



PHP Array Functions

array()

The array() function is used to create an array.
Syntax for one dimensional array:
array(value1, value2, value3, ….);

Eg:
<?php
$arr = array("Apple","Orange","Banana");
echo "Fruits in array are: ".$arr[0].",".$arr[1]." & ".$arr[2];
?>
Output:
Fruits in array are: Apple,Orange & Banana

array_pop()

The array_pop() function deletes the last element of an array.
Syntax:
array_pop(array);

Eg:
<?php
$arr = array("Apple","Orange","Banana");
array_pop($arr);
print_r($arr);
?>
Output:
Array ( [0] => Apple [1] => Orange )

array_push()

The array_push() function inserts one or more elements to the end of an array.
Syntax:
array_push(array, value1, value2, ….);

Eg:
<?php
$arr = array("Apple","Orange","Banana");
array_push($arr,"Kiwi","Mango");
print_r($arr);
?>
Output:
Array ( [0] => Apple [1] => Orange [2] => Banana [3] => Kiwi [4] => Mango )

array_reverse()

The array_reverse() function returns an array in the reverse order.
Syntax:
array_reverse(array);

Eg:
<?php
$arr = array("Apple","Orange","Banana");
$rev_arr = array_reverse($arr); /* returns reversed array and stores in $rev_arr
*/
print_r($rev_arr);
?>
Output:
Array ( [0] => Banana [1] => Orange [2] => Apple )

array_search()

The array_search() function searches an array for a value and returns the key.
Syntax:
array_search(value, array, true/false);

Eg:
<?php
$arr = array("Apple","Orange","Banana");
echo array_search("Banana",$arr); /* returns 2 which is the key for the element 'Banana'*/
?>
Output:
2

array_sum()

The array_sum() function returns the sum of all the values in the array.
Syntax:
array_sum(array);

Eg:
<?php
$numarr = array(10.5,20,4.5);
echo array_sum($numarr); // output - 35
?>
Output:
35

array_unique()

The array_unique() function removes duplicate values from an array.
Syntax:
array_unique(array, type);

Types:

  • SORT_STRING - Default value. Compare items as strings
  • SORT_REGULAR - Compare items normally
  • SORT_NUMERIC - Compare items numerically
  • SORT_LOCALE_STRING - Compare items as strings, based on current locale
Eg:
<?php
$arr = array("Apple","Orange","Banana","Apple", "Banana");
print_r(array_unique($arr));
?>
Output:
Array ( [0] => Apple [1] => Orange [2] => Banana )

count()

The count() function returns the number of elements in an array.
Syntax:
count(array);

Eg:
<?php
$arr = array("Apple","Orange","Banana");
echo count($arr);
?>
Output:
3

sizeof()

The sizeof() function returns the number of elements in an array.
Syntax:
sizeof(array);

Eg:
<?php
$arr = array("Apple","Orange","Banana","");
echo sizeof($arr);
?>
Output:
4

in_array()

The in_array() function used for checking whether a value exists or not in an array. The in_array() function returns true if the value exists in the array.
Syntax:
in_array(search_value, array);

Eg:
<?php
$arr = array("Apple","Orange","Banana");
if(in_array("Orange", $arr))
{
echo "Orange is in array.";
}
else
{
echo "Orange is not in array.";
}
?>
Output:
Orange is in the array.

sort()

The sort() function sorts an indexed array in ascending order.
Syntax:
sort(array, type); 'type' Specifies how to compare the array elements.

Eg:
<?php
$arr = array("Apple","Orange","Banana","");
sort($arr);
print_r($arr);
?>
Output:
Array ( [0] => [1] => Apple [2] => Banana [3] => Orange )