Below are some useful functions of php Array.Array is very important data type of any language.PHP also have good sets of functions to manipulate array.
sizeof($arr):
This function returns the number of elements in an array or size of array.Normally Use this function to find out how many elements in an array.This function normally use in loop or check array length.
1 2 3 4 5 6
$data = array("php", "jquery", "mysql");
echo "Size = " . sizeof($data);
?>
Output:
Size= 3
array_values($arr):
This function is used to retrieve all the values from an associative array. Its counterpart of the array_keys() function.
1 2 3 4 5
$data = array("fname" => "parvez", "lname" => "alam");
print_r(array_values($data));
?>
Output:
Array
(
[0] => parvez
[1] => alam
)
array_keys($arr):
This function is used to accepts retrieve all the keys from an associative array. Its counterpart of array_values() function.
1 2 3 4 5
$data = array("fname" => "parvez", "lname" => "alam");
print_r(array_keys($data));
?>
Output:
Array
(
[0] => fname
[1] => lname
)
array_pop($arr):
This function removes an element from the end of an array.
1 2 3 4 5 6 7
$data = array("parvez", "alam", "ansari");
array_pop($data);
print_r($data);
?>
Output:
Array
(
[0] => parvez
[1] => alam
)
array_push($arr, $val):
This function adds an element to the end of an array.
Code:
$data = array(“parvez”, “alam”, “ansari”);
array_push($data, “Nadeem”);
print_r($data);
?>
Output:
Array
(
[0] => parvez
[1] => alam
[2] => ansari
[3] => nadeem
)
array_shift($arr):
This function removes an element from the beginning of an array.
1 2 3 4 5 6 7
$data = array("parvez", "alam", "nadeem");
array_shift($data);
print_r($data);
?>
Output:
Array
(
[0] => alam
[1] => nadeem
)
array_unshift($arr, $val):
This function adds an element to the beginning of an array.
1 2 3 4 5 6 7
$data = array("parvez", "alam", "nadeem");
array_unshift($data, "sameer");
print_r($data);
?>
Output:
Array
(
[0] => sameer
[1] => parvez
[2] => alam
[3] => nadeem
)
each($arr):
This function is mostly used in loop to traverse an array. Each time each() is called, it returns the current key-value pair and moves the array cursor forward one element.
1 2 3 4 5 6 7 8 9
$data = array("fname" => "parvez", "lname" => "alam");
while (list($key, $value) = each($data)) {
echo "$key: $value \n";
}
?>
Output:
hero: Holmes
villain: Moriarty
sort($arr):
This function used to sorts the elements of an array in ascending order. The String values will be arranged in ascending alphabetical order.
Note: Other sorting functions include asort(), arsort(), ksort(), krsort() and rsort()
Code:
$data = array(“c”, “d”, “a”, “b”);
sort($data);
print_r($data);
?>
Output:
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
array_flip($arr):
The function exchanges the keys and values of a PHP associative array.Use this function if you want to interchange the key and value pair of an array..
1 2 3 4 5
$data = array("a" => "parvez", "b" => "alam");
print_r(array_flip($data));
?>
Output:
Array
(
[parvez] => a
[alam] => b
)
array_reverse($arr):
The function reverses the order of elements in an array.Use this function to re-order a sorted list of values in reverse.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
$data = array(10, 20, 25, 60);
print_r(array_reverse($data));
?>
<span class="green">Output:</span>
Array
(
[0] => 60
[1] => 25
[2] => 20
[3] => 10
)
<h6>array_merge($arr):</h6>
This function merges two or more arrays to create a single composite array. Key collisions are resolved in favor of the latest entry in array.array_merge($arr) is used to merge two array into single array.
[code type="php"]
$data1 = array("fname", "lname");
$data2 = array("address", "phn");
print_r(array_merge($data1, $data2));
?>
array_merge($arr)
Output:
Array
(
[0] => fname
[1] => lname
[2] => address
[3] => phn
)
array_rand($arr):
This function selects one or more random elements from an array.This function is used when you want to get random number from collections of number.
1 2 3 4 5 6 7 8
$data = array("php", "tutorial", "jquery");
echo "random selected = " . $data[array_rand($data)];
?>
array_merge($arr)
Output:
random selected = php
array_search($search, $arr):
This function searches the values in an array for a match to the search term, and returns the corresponding key if found. If more than one match exists, the key of the first matching value is returned.
Code:
$data = array("blue" => "#0000cc", "black" => "#000000", "green" => "#00ff00");
echo "Found in array= " . array_search("#0000cc", $data);
?>
Output:
Found in array= blue
array_slice($arr, $offset, $length)
This function is useful to extract a subset of the elements of an array, as another array. Extracting begins from array offset $offset and continues until the array slice is $length elements long.
Use this function to break a larger array into smaller array or chunk of array.
1 2 3 4 5
$data = array("vanilla", "strawberry", "mango", "peaches");
print_r(array_slice($data, 1, 2));
?>
Output:
Array
(
[0] => strawberry
[1] => mango
)
array_unique($data):
This function is used to get unique elements from an array.You can use this function when you need to remove non-unique elements from an array.
1 2 3 4 5
$data = array(1,1,4,6,7,4); print_r(array_unique($data)); ?>
Output:
Array
(
[0] => 1
[3] => 6
[4] => 7
[5] => 4
)
array_walk($arr, $func):
This is very important function of array when you need to perform custom processing on every element of an array.This function apply applying a user-defined function to every element. It returns the changed array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
function reduceBy10(&$val, $key) {
$val -= $val * 0.1;
}
$data = array(10,20,30,40);
array_walk($data, 'reduceBy10');
print_r($data);
?>
Output:
Array
(
[0] => 9
[1] => 18
[2] => 27
[3] => 36
)