1. array_count_values() - Counts all the values of an array
array_count_values() returns an array using the values of array as keys and their frequency in array as values.
array array_count_values(array $array)
Returns an associative array of values from array as keys and their count as value.
Parametes:
array
: The array of values to count.
<?php
$array = array(1,"India", 1, "India", 3);
print_r(array_count_values($array));
?>
Output:
Array
(
[1] => 2
[India] => 2
[3] => 1
)
2. count() - Count all elements in an array, or something in an object
Counts all elements in an array, or something in an object.
int count(mixed $array_or_countable [, int $mode = COUNT_NORMAL ])
Returns an associative array of values from array as keys and their count as value.
Parametes:
array_or_countable
: An array or Countable object.
mode
: If the optional mode parameter is set to COUNT_RECURSIVE (or 1), count() will recursively count the array. This is particularly useful for counting all the elements of a multidimensional array.
<?php
$array = array(1,"India", 1, "India", 3);
echo count($array);
?>
Output:
3. array_values() - Return all the values of an array
array_values() returns all the values from the array and indexes the array numerically.
Returns an indexed array of values.
array array_values(array $array)
Parametes:
<?php
$array = array("Size" => "L", "Color" => "Green");
print_r(array_values($array));
?>
Output:
Array
(
[0] => L
[1] => Green
)
4. sort() - Sort an array
This function sorts an array. Elements will be arranged from lowest to highest when this function has completed.
Returns an indexed array of values.
bool sort(array &$array [, int $sort_flags = SORT_REGULAR ])
Parametes:
array
: The array.The input array.
sort_flags
: The optional second parameter sort_flags may be used to modify the sorting behavior using these value:
- SORT_REGULAR - compare items normally (don't change types)
- SORT_NUMERIC - compare items numerically
- SORT_STRING - compare items as strings
- SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()
- SORT_NATURAL - compare items as strings using "natural ordering" like natsort()
- SORT_FLAG_CASE - can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively
<?php
$array = array("Mango", "Banana", "Strawberry", "Apple");
sort($array);
print_r($array);
?>
Output:
Array
(
[0] => Apple
[1] => Banana
[2] => Mango
[3] => Strawberry
)
5. asort() - Sort an array and maintain index association
This function sorts an array such that array indices maintain their correlation with the array elements they are associated with.
This is used mainly when sorting associative arrays where the actual element order is significant.
Returns TRUE on success or FALSE on failure.
bool asort(array &$array [, int $sort_flags = SORT_REGULAR ])
Parametes:
array
: The array.The input array.
sort_flags
: You may modify the behavior of the sort using the optional parameter sort_flags, for details see sort().
<?php
$array = array("d" => "Mango", "b" => "Banana", "c" => "Strawberry", "d" => "Apple");
sort($array);
print_r($array);
?>
Output:
Array
(
[d] => Apple
[b] => Banana
[c] => Strawberry
)
6. ksort() - Sort an array by key
Sorts an array by key, maintaining key to data correlations. This is useful mainly for associative arrays.
Returns TRUE on success or FALSE on failure.
bool asort(array &$array [, int $sort_flags = SORT_REGULAR ])
Parametes:
array
: The array.The input array.
sort_flags
: You may modify the behavior of the sort using the optional parameter sort_flags, for details see sort().
<?php
$array = array("d" => "Mango", "b" => "Banana", "c" => "Strawberry", "d" => "Apple");
ksort($array);
print_r($array);
?>
Output:
Array
(
[b] => Banana
[c] => Strawberry
[d] => Apple
)
7. krsort() - Sort an array by key in reverse order
Sorts an array by key in reverse order, maintaining key to data correlations. This is useful mainly for associative arrays.
Returns TRUE on success or FALSE on failure.
bool asort(array &$array [, int $sort_flags = SORT_REGULAR ])
Parametes:
array
: The array.The input array.
sort_flags
: You may modify the behavior of the sort using the optional parameter sort_flags, for details see sort().
<?php
$array = array("d" => "Mango", "b" => "Banana", "c" => "Strawberry", "d" => "Apple");
krsort($array);
print_r($array);
?>
Output:
Array
(
[d] => Apple
[c] => Strawberry
[b] => Banana
)
8. array_reverse() - Return an array with elements in reverse order
Takes an input array and returns a new array with the order of the elements reversed.
Returns the reversed array.
array array_reverse (array &$array [, bool $preserve_keys = FALSE ])
Parametes:
array
: The array.The input array.
preserve_keys
: If set to TRUE numeric keys are preserved. Non-numeric keys are not affected by this setting and will always be preserved.
<?php
$array = array("Mango", "Banana", "Strawberry", "Apple");
print_r(array_reverse($array));
?>
Output:
Array
(
[0] => Apple
[1] => Strawberry
[2] => Banana
[3] => Mango
)