Contents

PHP Array Functions

PHP Array Functions

array_chunk()

Description:Splits an array into chunks of a specified size.

Example:

				
					$array = array(1, 2, 3, 4, 5, 6);
print_r(array_chunk($array, 2));
// Output:
// Array
// (
//     [0] => Array ( [0] => 1 [1] => 2 )
//     [1] => Array ( [0] => 3 [1] => 4 )
//     [2] => Array ( [0] => 5 [1] => 6 )
// )

				
			

array_combine()

Description:Creates an array by combining one array as keys and another array as values.

Example:

				
					$keys = array('fruit', 'vegetable');
$values = array('apple', 'carrot');
print_r(array_combine($keys, $values));
// Output:
// Array
// (
//     [fruit] => apple
//     [vegetable] => carrot
// )

				
			

array_count_values()

Description:Counts the occurrences of all values in an array.

Example:

				
					$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
// Output:
// Array
// (
//     [1] => 2
//     [hello] => 2
//     [world] => 1
// )

				
			

array_diff()

Description:Computes the difference between two or more arrays.

Example:

				
					$array1 = array("a" => "green", "b" => "brown", "c" => "blue");
$array2 = array("a" => "green", "yellow", "red");
print_r(array_diff($array1, $array2));
// Output:
// Array
// (
//     [b] => brown
//     [c] => blue
// )

				
			

array_fill()

Description:Fills an array with a specified value, starting from a specified index.

Example:

				
					print_r(array_fill(3, 5, "apple"));
// Output:
// Array
// (
//     [3] => apple
//     [4] => apple
//     [5] => apple
//     [6] => apple
//     [7] => apple
// )

				
			

array_filter()

Description:Filters elements of an array using a callback function.

Example:

				
					$array = array(1, 2, 3, 4, 5, 6);
print_r(array_filter($array, function($value) { return $value % 2 == 0; }));
// Output:
// Array
// (
//     [1] => 2
//     [3] => 4
//     [5] => 6
// )

				
			

array_flip()

Description:Exchanges keys and values in an array.

Example:

				
					$array = array("a" => "apple", "b" => "banana", "c" => "cherry");
print_r(array_flip($array));
// Output:
// Array
// (
//     [apple] => a
//     [banana] => b
//     [cherry] => c
// )

				
			

array_key_exists()

Description:Checks if a specified key exists within an array.

Example:

				
					$array = array("fruit" => "apple", "vegetable" => "carrot");
echo array_key_exists("fruit", $array) ? "Key exists" : "Key does not exist";
// Output: Key exists

				
			

array_merge()

Description:Merges one or more arrays into a single array.

Example:

				
					$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
print_r(array_merge($array1, $array2));
// Output:
// Array
// (
//     [color] => green
//     [0] => 2
//     [1] => 4
//     [2] => a
//     [3] => b
//     [shape] => trapezoid
//     [4] => 4
// )

				
			

array_reverse()

Description:Reverses the order of elements in an array.

Example:

				
					$array = array("a", "b", "c", "d");
print_r(array_reverse($array));
// Output:
// Array
// (
//     [0] => d
//     [1] => c
//     [2] => b
//     [3] => a
// )

				
			

array_search()

Description:Searches for a value in an array and returns the corresponding key if found.

Example:

				
					$array = array(0 => 'blue', 1 => 'red', 2 => 'green');
echo array_search('green', $array);
// Output: 2

				
			

array_sum()

Description:Returns the sum of all values in an array.

Example:

				
					$array = array(2, 4, 6, 8);
echo array_sum($array);
// Output: 20

				
			

array_unique()

Description:Removes duplicate values from an array.

Example:

				
					$array = array("apple", "banana", "apple", "orange");
print_r(array_unique($array));
// Output:
// Array
// (
//     [0] => apple
//     [1] => banana
//     [3] => orange
// )

				
			

array_values()

Description:Returns all the values from an array, with numeric keys starting from 0.

Example:

				
					$array = array("size" => "large", "color" => "red");
print_r(array_values($array));
// Output:
// Array
// (
//     [0] => large
//     [1] => red
// )

				
			

compact()

Description:Create an array using variables as keys and their values as elements.

Example:

				
					$name = "John";
$age = 30;
print_r(compact("name", "age"));
// Output: Array ( [name] => John [age] => 30 )

				
			

count()

Description:Count the total number of elements in an array.

Example:

				
					$array = array("apple", "banana", "cherry");
echo count($array);
// Output: 3

				
			

current()

Description:Return the value of the element at the internal pointer in an array.

Example:

				
					$array = array("apple", "banana", "cherry");
echo current($array);
// Output: apple

				
			

end()

Description:Find the last element of a given array.

Example:

				
					$array = array("apple", "banana", "cherry");
echo end($array);
// Output: cherry

				
			

extract()

Description:Convert array elements into variables.

Example:

				
					$array = array("name" => "John", "age" => 30);
extract($array);
echo $name;
// Output: John

				
			

in_array()

Description:Check if a specific value exists in an array.

Example:

				
					$array = array("apple", "banana", "cherry");
echo in_array("banana", $array) ? "Found" : "Not found";
// Output: Found

				
			

key()

Description:Return the current index or key of an array where the internal pointer points.

Example:

				
					$array = array("a" => "apple", "b" => "banana");
echo key($array);
// Output: a

				
			

krsort()

Description:Sort an array by its keys in descending order.

Example:

				
					$array = array("a" => "apple", "c" => "cherry", "b" => "banana");
krsort($array);
print_r($array);
// Output: Array ( [c] => cherry [b] => banana [a] => apple )

				
			

list()

Description:Assign array values to multiple variables.

Example:

				
					$array = array("apple", "banana", "cherry");
list($fruit1, $fruit2) = $array;
echo $fruit1;
// Output: apple

				
			

natcasesort()

Description:Sort an array using natural order, case-insensitively.

Example:

				
					$array = array("Image2.jpg", "image10.jpg", "Image1.jpg");
natcasesort($array);
print_r($array);
// Output: Array ( [0] => Image1.jpg [2] => Image2.jpg [1] => image10.jpg )

				
			

natsort()

Description:Sort an array using a “natural order” algorithm without regard to case.

Example:

				
					$array = array("Image2.jpg", "image10.jpg", "Image1.jpg");
natsort($array);
print_r($array);
// Output: Array ( [2] => Image1.jpg [0] => Image2.jpg [1] => image10.jpg )

				
			

next()

Description:Move the internal pointer to the next element and return its value.

Example:

				
					$array = array("apple", "banana", "cherry");
echo next($array);
// Output: banana

				
			

pos()

Description:Return the value of the current element of an array.

Example:

				
					$array = array("apple", "banana", "cherry");
echo pos($array);
// Output: apple

				
			

prev()

Description:Move the internal pointer to the previous element and return its value.

Example:

				
					$array = array("apple", "banana", "cherry");
next($array);
echo prev($array);
// Output: apple

				
			

range()

Description:Create an array with elements within a specified range.

Example:

				
					print_r(range(0, 5));
// Output: Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )

				
			

reset()

Description:Move the internal pointer to the first element of an array.

Example:

				
					$array = array("apple", "banana", "cherry");
echo reset($array);
// Output: apple

				
			

rsort()

Description:Sort an array in descending order.

Example:

				
					$array = array(3, 1, 2);
rsort($array);
print_r($array);
// Output: Array ( [0] => 3 [1] => 2 [2] => 1 )

				
			

shuffle()

Description:Randomize the order of elements in an array.

Example:

				
					$array = array("apple", "banana", "cherry");
shuffle($array);
print_r($array);
// Output: (random order, e.g., Array ( [0] => banana [1] => cherry [2] => apple ))

				
			

sizeof()

Description:Count the number of elements in an array or countable object.

Example:

				
					$array = array("apple", "banana", "cherry");
echo sizeof($array);
// Output: 3

				
			

sort()

Description:Sort an array in ascending order.

Example:

				
					$array = array(3, 1, 2);
sort($array);
print_r($array);
// Output: Array ( [0] => 1 [1] => 2 [2] => 3 )

				
			

uasort()

Description:Sort an array while maintaining index association, using a user-defined function.

Example:

				
					$array = array("a" => 3, "b" => 1, "c" => 2);
uasort($array, function($x, $y) { return $x <=> $y; });
print_r($array);
// Output: Array ( [b] => 1 [c] => 2 [a] => 3 )

				
			

usort()

Description:Sort an array using a custom comparison function.

Example:

				
					$array = array("apple", "banana", "cherry");
usort($array, function($x, $y) { return strlen($x) <=> strlen($y); });
print_r($array);
// Output: Array ( [0] => apple [1] => cherry [2] => banana )