Hi,
As we have discussed there are a lot of functions in PHP for arrays. I have broken down this post in several parts to cover them all up. Just read on to know more about these functions.
array_change_key_case():-The array_change_key_case() function returns an array with all array KEYS in lower case or upper case.
Example:- <?php
$a=array("a"=>"First","b"=>"Second","c"=>"Third");
print_r(array_change_key_case($a,CASE_UPPER));
?>
Output:- Array (
[A] => Cat [B] => Dog [C] => Horse
)
array_chunk():-The array_chunk() function splits an array into chunks of new arrays.
Example:-<?php
$a=array("a"=>"First","b"=>"Second","c"=>"Third","d"=>"Four");
print_r(array_chunk($a,2));
?>
Output:-Array (
[0] => Array ( [0] => First[1] => Second)
[1] => Array ( [0] => Third[1] => Four)
)
array_combine():-The array_combine() PHP array function will combine two arrays and create an associative array out of them by making the first array the keys, and the second array the values. This function takes two parameters. First parameter is the array to be used as keys, and the second parameter is the array to be used as values.
Example:- <?php
$array1 = array("first","second");
$array2 = array("1","2");
$newArray = array_combine($array1, $array2);
foreach ($newArray as $key => $value) {
echo "$key - <strong>$value</strong> <br />";
}
?>
Output:- first-1
second-2
array_count_values():-The array_count_values() function returns an associative array of values from input as keys and their count as value.
Example:-<?php
$a=array("Cat","Dog","Horse","Dog");
print_r(array_count_values($a));
?>Output:-Array (
[Cat] => 1 [Dog] => 2 [Horse] => 1
)
array_diff_assoc():-The array_diff_assoc() function compares two or more array,and returns the diffrence of the array with there key.
Example:-<?php
$a1=array(0=>"First",1=>"Second";,2=>"Third");
$a2=array(0=>"Four",1=>"Third";,2=>"Second");
$a3=array(0=>"Third",1=>"Second",2=>"First");
print_r(array_diff_assoc($a1,$a2,$a3));
?>
Output:-Array (
[0] => First[2] => Four
).
array_diff_key:-array_diff_key():- function returns difference of arrays using keys for comparison
Example:-<?php
$a1=array(0=>"First",1=>"Second";,2=>"Third");
$a2=array(2=>"Four",3=>"Five";,4=>"Six");
$a3=array(5=>"Third",6=>"Second",7=>"Four");
print_r(array_diff_assoc($a1,$a2,$a3));
?>
Output:-Array (
[0] => First[2] => Second
)
array_diff_assoc():-The array_diff_assoc() function compares two or more arrays, and returns an array values and their keys from the first array, only if they are not present in any of the other arrays.
array_diff_ukey():-The array_diff_key() function compares two or more arrays, and returns an array values with the keys from the first array, only if the key is not present in any of the other arrays.
array_diff():-The array_diff() function compares two or more arrays, and returns an array values with the keys from the first array, only if the value is not present in any of the other arrays.
Example:<?php
$a1=array(0=>"First",1=>"Second",2=>"Third");
$a2=array(3=>"Third",4=>"Second",5=>"Five");
print_r(array_diff($a1,$a2));
?>
Output:-Array (
[0] => First
)
array_fill_keys():-array_fill_keys Fill an array with values,with specifying there keys
Example:-<?php
$keys = array('Abc',7);
$a = array_fill_keys($keys, 'Xyz');
print_r($a);
?>
Output:-Array
(
[Abc] => Xyz[7] => Xyz
)
array_fill():-The array_fill() function returns an array filled with the values
Example:-<?php
$a=array_fill(2,3,"First");
print_r($a);
?>
Output:-Array (
[2] => First[3] => First[4] => First
)
array_filter():-The array_filter() function passes each value in the array to a user-made function, which returns either true or false, and returns values with the an array.
Example:-<?php
function testfunction($v){
if ($v==="First"){
return true; }
return false; }
$a=array(0=>"Second",1=>"Third",2=>"First");
print_r(array_filter($a,"testfunction"));
?>
Output:-Array (
[2] => First
)
array_flip():-The array_flip() function returns an array values with all the original keys, and all original values as keys
Example:-<?php
$a=array(0=>"First",1=>"Second",2=>"Third");
print_r(array_flip($a));
?>
Output:-Array (
[First] => 0 [Second] => 1 [Third] => 2
)
array_intersect_assoc(): -The array_intersect_assoc() function compares two or more arrays, and returns an array values with the keys from the first array, only if they are present in all of the other arrays.
Example:- <?php
$a1=array(0=>"First",1=>"Second",2=>"Third");
$a2=array(3=>"Third",1=>"Five",0=>"First");
print_r(array_intersect_assoc($a1,$a2));
?>
Output:- Array (
[0] => First[1] => Third
)
array_intersect_key():-The array_intersect_key() function compares two or more arrays, and returns an array values with the keys from the first array, only if the key is present in all of the other arrays.
Example:-<?php
$a1=array(0=>"First",1=>"Second",2=>"Third");
$a2=array(2=>"Four",0=>"First",4=>"Five");
print_r(array_intersect_key($a1,$a2));
?>
Output:- Array (
[0] => First[2] => Third
)
array_intersect_uassoc():-The array_intersect_assoc() function compares two or more arrays, and returns an array and values with the keys from the first array, only if they are present in all of the other arrays.
Example:- <?php
$a1=array(0=>"First",1=>"Second",2=>"Third");
$a2=array(3=>"Four",1=>"Second",0=>"First");
print_r(array_intersect_assoc($a1,$a2));
?>
Output:- Array (
[0] => First[1] => Second
)
array_intersect_ukey():-The array_intersect_key() function compares two or more arrays, and returns an array values with the keys from the first array, but only if the key is present in all of the other arrays
Example:- <?php
$a1=array(0=>"First",1=>"Second",2=>"Third");
$a2=array(2=>"Four",0=>"First",4=>"Five");
print_r(array_intersect_key($a1,$a2));
?>
Output:- Array (
[0] => First[2] => Third
)
array_intersect():- The array_intersect() function compares two or more arrays,and returns an array values with the keys from the first array,but only if the value is present in all of the other arrays.
Example:-<?php
$a1=array(0=>"First",1=>"Second",2=>"Third");
$a2=array(3=>"Third",4=>"Second",5=>"Four");
print_r(array_intersect($a1,$a2));
?>
Output:- Array (
[1] => Second[2] => Third
)
array_key_exists():-The array_key_exists() function checks an array for a specified key, and function returns true if the key exists and false if the key does not exist.
Example:-<?php
$a=array("First",Second");
if (array_key_exists(“F”,$a)){
echo "Key exists!";
}else
{
echo "Key does not exist!";
}
?>
Output:-Key exists!
array_keys():-The array_keys() function returns keys with there array.
Example:-<?php
$a=array("a"=>"First","b"=>"Second","c"=>"Third");
print_r(array_keys($a));
?>
Output:-Array (
[0] => a [1] => b [2] => c
)
array_map():-The array_map() function sends each value of an array to a user-define function, and returns an array with new values, given by the user-define function.
Example:-<?php
function myfunction($v)
{
if ($v==="First")
{
return "Five";
}
return $v;
}
$a=array("Third","First","Four");
print_r(array_map("myfunction",$a));
?>
Output:-Array (
[0] => Third[1] => Five[2] => Four
)
array_merge_recursive():-The array_merge_recursive() function merges one ore more arrays into one array.
Example:-<?php
$a1=array("a"=>"First","b"=>"Second");
$a2=array("c"=>"Third","b"=>"Four");
print_r(array_merge_recursive($a1,$a2));
?>
Output:-Array (
[a] => First
[b] => Array ( [0] => Second[1] => Four)
[c] => Third
)
array_merge():- The array_merge() function merges one ore more arrays into one array.
Example:-<?php
$a1=array("a"=>"First","b"=>"Second");
$a2=array("c"=>"Third","b"=>"Four");
print_r(array_merge($a1,$a2));
?>
Output:-Array ( [a] => First[b] => Four[c] => Third)
array_multisort ():-The array_multisort() function returns a sorted array with there values and keys.The function sorts the first array, and the other arrays follow the first array, then, if two or more values are the same, it sorts the next array, and so on.
Example:-<?php
$a1=array("First","Second");
$a2=array("Third","Four");
array_multisort($a1,$a2);
print_r($a1);
print_r($a2);
?>
Output:-Array (
[0] => Second[1] => First
)
Array (
[0] => Four[1] => Third
)
array_pad ():-The array_pad() function inserts a specific number of elements, with a specific value, to an array.
Example:-<?php
$a=array("Dog","Cat");
print_r(array_pad($a,5,0));
?>
Output:-Array (
[0] => Dog [1] => Cat [2] => 0 [3] => 0 [4] => 0
)
There are so many more. Let's continue the list in next post...
Thanks for reading.
--
Amit Kriplani
PHP Web/Application Developer
Blog:http://blog.amitkriplani.com
Mobile: +91-9913932644MSN: amitk87@hotmail.com
Gmail: amitkriplani5@gmail.com
Yahoo: amitkriplani@ymail.com
Skype: amitkriplani
Twitter: @amitk87
As we have discussed there are a lot of functions in PHP for arrays. I have broken down this post in several parts to cover them all up. Just read on to know more about these functions.
array_change_key_case():-The array_change_key_case() function returns an array with all array KEYS in lower case or upper case.
Example:- <?php
$a=array("a"=>"First","b"=>"Second","c"=>"Third");
print_r(array_change_key_case($a,CASE_UPPER));
?>
Output:- Array (
[A] => Cat [B] => Dog [C] => Horse
)
array_chunk():-The array_chunk() function splits an array into chunks of new arrays.
Example:-<?php
$a=array("a"=>"First","b"=>"Second","c"=>"Third","d"=>"Four");
print_r(array_chunk($a,2));
?>
Output:-Array (
[0] => Array ( [0] => First[1] => Second)
[1] => Array ( [0] => Third[1] => Four)
)
array_combine():-The array_combine() PHP array function will combine two arrays and create an associative array out of them by making the first array the keys, and the second array the values. This function takes two parameters. First parameter is the array to be used as keys, and the second parameter is the array to be used as values.
Example:- <?php
$array1 = array("first","second");
$array2 = array("1","2");
$newArray = array_combine($array1, $array2);
foreach ($newArray as $key => $value) {
echo "$key - <strong>$value</strong> <br />";
}
?>
Output:- first-1
second-2
array_count_values():-The array_count_values() function returns an associative array of values from input as keys and their count as value.
Example:-<?php
$a=array("Cat","Dog","Horse","Dog");
print_r(array_count_values($a));
?>Output:-Array (
[Cat] => 1 [Dog] => 2 [Horse] => 1
)
array_diff_assoc():-The array_diff_assoc() function compares two or more array,and returns the diffrence of the array with there key.
Example:-<?php
$a1=array(0=>"First",1=>"Second";,2=>"Third");
$a2=array(0=>"Four",1=>"Third";,2=>"Second");
$a3=array(0=>"Third",1=>"Second",2=>"First");
print_r(array_diff_assoc($a1,$a2,$a3));
?>
Output:-Array (
[0] => First[2] => Four
).
array_diff_key:-array_diff_key():- function returns difference of arrays using keys for comparison
Example:-<?php
$a1=array(0=>"First",1=>"Second";,2=>"Third");
$a2=array(2=>"Four",3=>"Five";,4=>"Six");
$a3=array(5=>"Third",6=>"Second",7=>"Four");
print_r(array_diff_assoc($a1,$a2,$a3));
?>
Output:-Array (
[0] => First[2] => Second
)
array_diff_assoc():-The array_diff_assoc() function compares two or more arrays, and returns an array values and their keys from the first array, only if they are not present in any of the other arrays.
array_diff_ukey():-The array_diff_key() function compares two or more arrays, and returns an array values with the keys from the first array, only if the key is not present in any of the other arrays.
array_diff():-The array_diff() function compares two or more arrays, and returns an array values with the keys from the first array, only if the value is not present in any of the other arrays.
Example:<?php
$a1=array(0=>"First",1=>"Second",2=>"Third");
$a2=array(3=>"Third",4=>"Second",5=>"Five");
print_r(array_diff($a1,$a2));
?>
Output:-Array (
[0] => First
)
array_fill_keys():-array_fill_keys Fill an array with values,with specifying there keys
Example:-<?php
$keys = array('Abc',7);
$a = array_fill_keys($keys, 'Xyz');
print_r($a);
?>
Output:-Array
(
[Abc] => Xyz[7] => Xyz
)
array_fill():-The array_fill() function returns an array filled with the values
Example:-<?php
$a=array_fill(2,3,"First");
print_r($a);
?>
Output:-Array (
[2] => First[3] => First[4] => First
)
array_filter():-The array_filter() function passes each value in the array to a user-made function, which returns either true or false, and returns values with the an array.
Example:-<?php
function testfunction($v){
if ($v==="First"){
return true; }
return false; }
$a=array(0=>"Second",1=>"Third",2=>"First");
print_r(array_filter($a,"testfunction"));
?>
Output:-Array (
[2] => First
)
array_flip():-The array_flip() function returns an array values with all the original keys, and all original values as keys
Example:-<?php
$a=array(0=>"First",1=>"Second",2=>"Third");
print_r(array_flip($a));
?>
Output:-Array (
[First] => 0 [Second] => 1 [Third] => 2
)
array_intersect_assoc(): -The array_intersect_assoc() function compares two or more arrays, and returns an array values with the keys from the first array, only if they are present in all of the other arrays.
Example:- <?php
$a1=array(0=>"First",1=>"Second",2=>"Third");
$a2=array(3=>"Third",1=>"Five",0=>"First");
print_r(array_intersect_assoc($a1,$a2));
?>
Output:- Array (
[0] => First[1] => Third
)
array_intersect_key():-The array_intersect_key() function compares two or more arrays, and returns an array values with the keys from the first array, only if the key is present in all of the other arrays.
Example:-<?php
$a1=array(0=>"First",1=>"Second",2=>"Third");
$a2=array(2=>"Four",0=>"First",4=>"Five");
print_r(array_intersect_key($a1,$a2));
?>
Output:- Array (
[0] => First[2] => Third
)
array_intersect_uassoc():-The array_intersect_assoc() function compares two or more arrays, and returns an array and values with the keys from the first array, only if they are present in all of the other arrays.
Example:- <?php
$a1=array(0=>"First",1=>"Second",2=>"Third");
$a2=array(3=>"Four",1=>"Second",0=>"First");
print_r(array_intersect_assoc($a1,$a2));
?>
Output:- Array (
[0] => First[1] => Second
)
array_intersect_ukey():-The array_intersect_key() function compares two or more arrays, and returns an array values with the keys from the first array, but only if the key is present in all of the other arrays
Example:- <?php
$a1=array(0=>"First",1=>"Second",2=>"Third");
$a2=array(2=>"Four",0=>"First",4=>"Five");
print_r(array_intersect_key($a1,$a2));
?>
Output:- Array (
[0] => First[2] => Third
)
array_intersect():- The array_intersect() function compares two or more arrays,and returns an array values with the keys from the first array,but only if the value is present in all of the other arrays.
Example:-<?php
$a1=array(0=>"First",1=>"Second",2=>"Third");
$a2=array(3=>"Third",4=>"Second",5=>"Four");
print_r(array_intersect($a1,$a2));
?>
Output:- Array (
[1] => Second[2] => Third
)
array_key_exists():-The array_key_exists() function checks an array for a specified key, and function returns true if the key exists and false if the key does not exist.
Example:-<?php
$a=array("First",Second");
if (array_key_exists(“F”,$a)){
echo "Key exists!";
}else
{
echo "Key does not exist!";
}
?>
Output:-Key exists!
array_keys():-The array_keys() function returns keys with there array.
Example:-<?php
$a=array("a"=>"First","b"=>"Second","c"=>"Third");
print_r(array_keys($a));
?>
Output:-Array (
[0] => a [1] => b [2] => c
)
array_map():-The array_map() function sends each value of an array to a user-define function, and returns an array with new values, given by the user-define function.
Example:-<?php
function myfunction($v)
{
if ($v==="First")
{
return "Five";
}
return $v;
}
$a=array("Third","First","Four");
print_r(array_map("myfunction",$a));
?>
Output:-Array (
[0] => Third[1] => Five[2] => Four
)
array_merge_recursive():-The array_merge_recursive() function merges one ore more arrays into one array.
Example:-<?php
$a1=array("a"=>"First","b"=>"Second");
$a2=array("c"=>"Third","b"=>"Four");
print_r(array_merge_recursive($a1,$a2));
?>
Output:-Array (
[a] => First
[b] => Array ( [0] => Second[1] => Four)
[c] => Third
)
array_merge():- The array_merge() function merges one ore more arrays into one array.
Example:-<?php
$a1=array("a"=>"First","b"=>"Second");
$a2=array("c"=>"Third","b"=>"Four");
print_r(array_merge($a1,$a2));
?>
Output:-Array ( [a] => First[b] => Four[c] => Third)
array_multisort ():-The array_multisort() function returns a sorted array with there values and keys.The function sorts the first array, and the other arrays follow the first array, then, if two or more values are the same, it sorts the next array, and so on.
Example:-<?php
$a1=array("First","Second");
$a2=array("Third","Four");
array_multisort($a1,$a2);
print_r($a1);
print_r($a2);
?>
Output:-Array (
[0] => Second[1] => First
)
Array (
[0] => Four[1] => Third
)
array_pad ():-The array_pad() function inserts a specific number of elements, with a specific value, to an array.
Example:-<?php
$a=array("Dog","Cat");
print_r(array_pad($a,5,0));
?>
Output:-Array (
[0] => Dog [1] => Cat [2] => 0 [3] => 0 [4] => 0
)
There are so many more. Let's continue the list in next post...
Thanks for reading.
--
Amit Kriplani
PHP Web/Application Developer
Blog:http://blog.amitkriplani.com
Mobile: +91-9913932644MSN: amitk87@hotmail.com
Gmail: amitkriplani5@gmail.com
Yahoo: amitkriplani@ymail.com
Skype: amitkriplani
Twitter: @amitk87