| 1 comments ]

Some of the common php array functions are explained with simple examples

* array() :
Creates an array
* array_count_values():
It returns an array, with ,the parent array's values as keys, and the values is the number of occurrences.

Example:




$alphas = array('A', 'B', 'C', 'D', 'B', 'C', 'C', 'D', 'E');

$result = array_count_values($alphas);
print_r($results);
echo "
The array alphas have $result[A] A's, $result[B] B's, $result[C] C's,$result[D] D's and $result[E] E's";


?>

Output:


Array ( [A] => 1 [B] => 2 [C] => 3 [D]=>2 [E]=>1 )
The array alphas have 1 A's, 2 B's, 3 C's,2 D's and 1 E's


* array_change_key_case :
returns an array with all array keys in specified case.
the array Constants CASE_LOWER and CASE_UPPER used with this function. The former returns the array key values in lower case(default case) the later returns the array key values in upper case.

Example:


$parent=array("a"=>"apple","B"=>"boy","c"=>"cat","d"=>"doll");
$result=array_change_key_case($parent,CASE_UPPER);
?>

Output:


Array ( [A] => apple[B] => boy [C] => cat [D]=>doll)


*array_chunk() : splits an array into new arrays.
This function has 3 parameters one is array itself second one is size of chunk and third one is preserve_key which determines whether to preserve the parent key or not .
true value preserves the keys and false does not preserve the keys.False is the default one.

Example:


$array=array("a"=>"apple","b"=>"boy","c"=>"cat","d"=>"doll");
$result1=array_chunk($array,2);
$result2=array_chunk($array,2,true);
echo "The result array1=> preserve_key=default ie false ";
print_r($result1);
echo "
The result array2=> preserve_key=true "

print_r($result2);
?>

Output:

The result array1=> preserve_key=default ie false
Array (
[0] => Array ( [0] => apple[1] => boy )
[1] => Array ( [0] => cat [1] => doll )
)
The result array2=> preserve_key=true

Array (
[0] => Array ( [a] => apple[b] => boy )
[1] => Array ( [c] => cat [d] => doll )
)

*array_flip :
this function will transposes the keys and values of an array

Example:

$array=array("a"=>"apple","b"=>"boy","c"=>"cat","d"=>"doll");

echo "Array before using the flip function
";

foreach($array as $key=>$value){

echo "
array[".$key."]=>".$value;

}
$flipped_array = array_flip($array);

echo "Array after using the flip function
";

foreach($flipped_array as $key=>$value){
echo "
flipped_array[".$key."]=>".$value;

}

Output:

Array before using the flip function

array[a]=>apple
array[b]=>boy
array[c]=>cat
array[d]=>doll

Array after using the flip function


flipped_array[apple]=>a
flipped_array[boy]=>b
flipped_array[cat]=>c
flipped_array[doll]=>d

* array_intersect :
this function returns an array containing elements that are present in all array arguments.
it returns with the key of first arry.

Example:

$array1 = array('apple', 'boy', 'cat', 'doll');
$array2 = array('doll','angel', 'bag', 'car');
$array3 = array('art', 'brain', 'doll', 'coal');
$intersect = array_intersect($array1, $array2, $array3);
echo "Intersection of array1, array2 and array3 is ";
print_r($intersection);
?>

output :
Intersection of array1, array2 and array3 is

Array
(
[3] => doll
)


* array_diff() :
this function returns an array with the keys and values from the first array, if the value is not present in the other arrays

* array_diff_assoc() :the function returns an array with the keys and values from the first array, only if they are not present in the other arrays

example :

$array1=array("a"=>"apple","b"=>"boy","c"=>"cat","d"=>"doll");
$array2=array("a"=>"apple","b"=>"bag",c=>"doll","d"=>"cat");
$result=array_diff($array1,$array2);
echo "
out put by array_diff()";

print_r($result);
echo "
out put by array_diff_assoc() ";

print_r(array_diff_assoc($array1,$array2));
?>
output:

out put by array_diff()";

Array (
[b] => boy
)


out put by array_diff_assoc()

Array (
[b] => boy
[c]=>cat
[d]=>doll
)

* array_combine() :
creates an array by merging two other arrays, with the first array as the keys, and the other as the values

example:


$array1=array("a","b","c","d");
$array22=array("apple","boy","cat","doll");
$result=array_combine($array1,$array2);
echo "merged array";
print_r($result);
?>
.output:

merged array

Array (
[a] => apple
[b] => boy
[c] => cat
[d] => doll

)



* array_keys() :
returns all valid keys for the given array.
* array_values() : returns an array containing all the values of an array

example:


$array1=array("a"=>"apple","b"=>"boy","c"=>"cat","d"=>"doll");
echo "
Array keys
", implode(", ", array_keys($array1));

echo "
Array values";

print_r(array_values($array1));

?>
Array keys

a, b,c,d

Array values
Array ( [0] =>apple [1] => boy [2] => cat [3]=>doll )

* array_merge()
:merges arrays into one array

example :

$array1=array("a"=>"apple","b"=>"doll");
$array2=array("c"=>"cat","b"=>"boy");
print_r(array_merge($array1,$array2));
?>
output

Array (
[a] => apple
[b] => boy
[c] => cat
)

* array_reverse() :returns an array in the reverse order .The second parameter determines whether to preserve the key of the parent array or not

example :

$array1=array("a"=>"apple","b"=>"boy","c"=>"cat","d"=>"doll");
print_r(array_reverse($array1));
?>
output

Array (
[d] => doll
[c] => cat
[b] => boy
[a]=>apple
)


*array_walk() : runs each array element in a user-defined function


example


function userFn($value,$key)
{
echo "array1[".$key."]=>".$value;
}
$array1=array("a"=>"apple","b"=>"doll");
array_walk($array1,"userFn");
?>


output :

array1[a]=>apple
array1[b]=>doll


*array_slice : returns an array subset of consecutive elements from the parent array




$array=array(0=>"apple",1=>"boy",2=>"cat",3=>"doll");
print_r(array_slice($array,-2,1));
echo "
";

print_r(array_slice($array,1,2,true));
?>

Array ( [0] => cat )

Array ( [1] => boy [2] => cat )

* arsort() : sorts an array by the values in reverse order
* ksort() : Sorts an array by key
* krsort() : Sorts an array by key in reverse order


$array = $array1=array2=array("a"=>"cat",b"=>"boy","c"=>"apple","d"=>"doll");
echo "arsort
";

arsort($array );
print_r($array );
echo "asort
";

asort($array1 );
print_r($array1 );
echo "ksort
";

ksort($array2 );
print_r($array2 );
?>
output
arsort
Array
(
[d]=>doll
[a] => cat
[b] => boy
[c] => apple
)

asort
Array
(
[c] => apple
[b] => boy
[a] => cat
[d]=>doll
)

ksort
Array
(
[a] => cat
[b] => boy
[c] => apple
[d]=>doll
)
krsort
Array
(
[d]=>doll
[c] => apple
[b] => boy
[a] => cat



)



* sizeof() : Returns the number of array elements.
* range() : Automatically create an array containing a range of elements
* in_array() : Returns true or false if an array contains a specific value.

example:

$array = array("a"=>"cat",b"=>"boy","c"=>"apple","d"=>"doll");
echo "size of array:".sizeof($array );
echo "
";

if (in_array("apple",$array ))
{
echo "Apple is in array";
}
else
{
echo "Apple is not in array";
}
?>
output :
sizeof array :4
* extract() : Extracts list items into matched variable/value pairs
* list() : Assigns list values to variables.
* shuffle() : Shuffles an array


$array1 = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
echo "Before shuffling ", implode(", ", $array1 ), "\n";
shuffle($array1 );
echo "After shuffling ", implode(", ", $array1 ), "\n";
?>

Output:

Before shuffling: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
After shuffling : 9, 4, 6, 3, 1, 5, 7, 8, 2, 10

1 comments

Anonymous said... @ Wednesday, February 3, 2010 at 1:35:00 AM GMT

Nice little list you've got here, thanks.

Post a Comment

Please put your comments here. your questions, your suggestions, also what went wrong with me.