PHP Array Functions |
What is array?
An array is a special variable that can store multiple values in a single variable. It is very easy to create and access. A set of information you can store using an array. Using an array you don't need to declare many variable.
by declaring array() function you can easily create an array.
$array = array();
Way to Store Value in an array:
$array = array('id' => 123, 'subject1' => 'Bangla', 'subject2' => 'English')
Why use Array Functions PHP?
In PHP, you can work with and manipulate arrays in different ways by using Array Functions. PHP arrays are important to the storage , management and execution of variables on sets. PHP supports simple and multidimensional arrays and can either be generated by the user or generated by another function.
How to Installation:
Array functions are part of the PHP core. That's why no need to installation to use these functions.There are many PHP Array Functions in following:
- array() Function - Use to create an array.
The php array() function is used to create an array.
In PHP, there are three types of arrays:
- Indexed arrays - Arrays with numeric index
- Associative arrays - Arrays with named keys
- Multidimensional arrays - Arrays containing one / more arrays
List of array Functions and descriptions:
- array() - Create an array.
- array_change_key_case() - Returns an array with all keys (lowercase or uppercase).
- array_chunk() - Splits / Divide an array into pieces of arrays.
- array_column() - In the input array return the values from a single column.
- array_combine() - It creates an array using one array for storing keys and another for storing its values.
- array_count_values()
- array_diff() - Returns the differences after Compares array values.
- array_diff_assoc() - Returns the differences after Compares array keys and values.
- array_diff_key() - Returns the differences after Compares array keys.
- array_diff_uassoc()
- array_diff_ukey()
- array_fill() - It fills an array with values.
- array_fill_keys()
- array_filter()
- array_flip()
- array_intersect() - Returns the matches after Compares array values.
- array_intersect_assoc() - Returns the matches after Compares array keys and values.
- array_intersect_key() - Returns the matches after Compares array keys.
- array_intersect_uassoc()
- array_intersect_ukey()
- array_key_exists() - Checks the specific key exists in the array.
- array_keys() - It returns all the keys of an array.
- array_map()
- array_merge() - It creates an array by merging one or more array.
- array_merge_recursive()
- array_multisort() - Used to Sort multiple or multi-dimensional arrays.
- array_pad()
- array_pop()
- array_product()
- array_push()
- array_rand()
- array_reduce()
- array_reverse()
- array_search()
- array_shift()
- array_slice()
- array_splice()
- array_sum()
- array_udiff()
- array_udiff_assoc()
- array_udiff_uassoc()
- array_uintersect()
- array_uintersect_assoc()
- array_uintersect_uassoc()
- array_unique()
- array_unshift()
- array_values()
- array_walk()
- array_walk_recursive()
- arsort()
- asort()
- compact()
- count()
- current()
- each()
- end()
- extract()
- in_array()
- key()
- krsort()
- ksort()
- list()
- natcasesort()
- natsort()
- next()
- pos()
- prev()
- range()
- reset()
- rsort()
- shuffle()
- sizeof()
- sort()
- uasort()
- uksort()
- usort()
In the following I have explained some PHP array functions.
- array_change_key_case() Function - This array function Changes all keys in an array to lowercase / uppercase.
array_change_key_case(array, CASE_LOWER/CASE_UPPER)
// CASE_LOWER - Default value. Changes the keys to lowercase.
//CASE_UPPER - Changes the keys to uppercase.
<!DOCTYPE html>
<html>
<body>
<?php
$array=array("Bangla"=>"05","Tutorials"=>"12","Point"=>"23"); print_r(array_change_key_case($array,CASE_UPPER));
?>
</body>
</html>
Result:
Array ( [BANGLA] => 05 [TUTORIALS] => 12 [POINT] => 23 )
- array_chunk() Function - This array function Splits an array into chunks of arrays.
<!DOCTYPE html>
<html>
<body>
<?php
$university=array("IUBAT","BUET","BUBT","DUET","DU","RUET");
echo "<pre>";
print_r(array_chunk($university,2));
?>
</body>
</html>
Result:
Array (
[0] => Array
(
[0] => IUBAT
[1] => BUET
)
[1] => Array
(
[0] => BUBT
[1] => DUET
)
[2] => Array
(
[0] => DU
[1] => RUET
)
)
- array_column() Function - This array function Returns the values from a single column in the input array.
Syntax:
array_column(array, column_key, index_key)
// array - required, column_key - required, index_key - Optional.
<?php
$a = array(
array(
'id' => 123,
'f_name' => 'Bangla',
'l_name' => 'English',
),
array(
'id' => 456,
'f_name' => 'Mathematics',
'l_name' => 'Physics',
),
array(
'id' => 789,
'f_name' => 'Biology',
'l_name' => 'Psychology',
)
);
$l_name = array_column($a, 'l_name');
print_r($l_name);
?>
Array (
[0] => English
[1] => Physics
[2] => Psychology
)
<?php
$a = array(
array(
'id' => 123,
'f_name' => 'Bangla',
'l_name' => 'English',
),
array(
'id' => 456,
'f_name' => 'Mathematics',
'l_name' => 'Physics',
),
array(
'id' => 789,
'f_name' => 'Biology',
'l_name' => 'Psychology',
)
);
$l_name = array_column($a, 'l_name', 'id');
print_r($l_name);
?>
Array (
[123] => English
[456] => Physics
[789] => Psychology
)
- array_combine() function - This array function Creates an array by using the elements from one "keys" array and one "values" array.
By using the elements from one "keys" array and one "values" array, Create an array :
<!DOCTYPE html>
<html>
<body>
<?php
$first_name=array("Bangla","Tutorials","Point");
$age=array("40","52","42");
$result=array_combine($first_name,$age);
print_r($result);
?>
</body>
</html>
Array (
[Bangla] => 40
[Tutorials] => 52
[Point] => 42
)
- array_count_values() Function - This array function Count all the values of an array:
<!DOCTYPE html>
<html>
<body>
<?php
$university=array("IUBAT","DU","IUBAT","DU","RUET"); print_r(array_count_values($university));
?>
</body>
</html>
Result:
Array (
[IUBAT] => 2
[DU] => 2
[RUET] => 1
)
- array_diff() Function - This array function Compare the values of two arrays, and return the differences:
<!DOCTYPE html>
<html>
<body>
<?php
$university1=array("IUBAT","BUET","DU");
$university2=array("IUBAT","DU"); $result=array_diff($university1,$university2);
print_r($result);
?>
</body>
</html>
Result:
Array (
[1] => BUET
)
- array_diff_assoc() Function - This array Function Compare arrays, and returns the differences (compare keys and values)
Compare the keys and values of two arrays, and return the differences:
<!DOCTYPE html>
<html>
<body>
<?php
$university1=array("a"=>"IUBAT","b"=>"BUET","c"=>"DU","d"=>"DUET"); $university2=array("a"=>"IUBAT","b"=>"BUET","c"=>"DU","e"=>"DUET"); $result=array_diff_assoc($university1,$university2);
print_r($result);
?>
</body>
</html>
Result:
Array (
[d] => DUET
)
- array_diff_key() Function - This array function Compare arrays, and returns the differences (compare keys only)
Return the differences after Compare the keys of two arrays:
<!DOCTYPE html>
<html>
<body>
<?php
$university1=array("a"=>"IUBAT","b"=>"BUET","c"=>"DU","d"=>"DUET"); $university2=array("a"=>"IUBAT","b"=>"BUET","e"=>"DUET"); $result=array_diff_key($university1,$university2);
print_r($result);
?>
</body>
</html>
Result:
Array (
[c] => DU
[d] => DUET
)
- array_diff_uassoc() Function - This array function Compare arrays, and returns the differences (compare keys and values, using a user-defined key comparison function)
- array_diff_ukey() Function - This function Compare arrays, and returns the differences (compare keys only, using a user-defined key comparison function)
- array_fill() Function - This array function Fills an array with values:
Return the differences after Compare the keys of two arrays:
<!DOCTYPE html>
<html>
<body>
<?php
$array1=array_fill(0,5,"IUBAT");
$array2=array_fill(0,1,"DUET");
print_r($array1);
echo "<br>";
print_r($array2);
?>
</body>
</html>
Result:
Array (
[0] => IUBAT
[1] => IUBAT
[2] => IUBAT
[3] => IUBAT
[4] => IUBAT
)
Array (
Array (
[0] => DUET
)
- array_fill_keys() Function - This array function Fill an array with values, specifying keys:
<!DOCTYPE html>
<html>
<body>
<?php
$keys=array("x","y","z","w");
$result=array_fill_keys($keys,"IUBAT");
print_r($result);
?>
</body>
</html>
Result:
Array (
[x] => IUBAT
[y] => IUBAT
[z] => IUBAT
[w] => IUBAT
)
- array_filter() Function - This array function Filter the values of an array using a callback function.
- array_flip() Function - This array function Flips/Exchanges all keys with their associated values in an array.
<!DOCTYPE html>
<html>
<body>
<?php
$university=array("x"=>"IUBAT","y"=>"BUET","z"=>"DUET","w"=>"RUET"); $result=array_flip($university);
print_r($result);
?>
</body>
</html>
Result:
Array (
[IUBAT] => x
[BUET] => y
[DUET] => z
[RUET] => w
)
EmoticonEmoticon