Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

PHP array functions part1

The document provides an overview of several PHP array functions including array_combine(), array_count_values(), array_reverse(), array_intersect(), and array_merge(). Each function is explained with its syntax, a brief description, and example code demonstrating its usage. The examples illustrate how to create, count, reverse, intersect, and merge arrays in PHP.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

PHP array functions part1

The document provides an overview of several PHP array functions including array_combine(), array_count_values(), array_reverse(), array_intersect(), and array_merge(). Each function is explained with its syntax, a brief description, and example code demonstrating its usage. The examples illustrate how to create, count, reverse, intersect, and merge arrays in PHP.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

PHP Array Functions

1. PHP array_combine() Function: Create an array by


using the elements from one "keys" array and one
"values" array. Both arrays must have equal number of elements!
Syntax
array_combine(keys, values)
<?php
$fname=array("Peter","Ben","Joe");
$age=array("35","37","43");
$c=array_combine($fname,$age);
print_r($c);
?>

Output: Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )


2. PHP array_count_values() Function: The
array_count_values() function counts all the values of an array.
<?php
$a=array("A","Cat","Dog","A","Dog");
print_r(array_count_values($a));
?>
Output: Array ( [A] => 2 [Cat] => 1 [Dog] => 2 )
3. PHP array_reverse() function: PHP array_reverse() function returns an
array containing elements in reversed order.

<?php
$season=array("summer","winter","spring","autumn");
$reverseseason=array_reverse($season);
foreach( $reverseseason as $s )
{
echo "$s<br />";
}
?>
Output:
autumn
spring
winter
summer
4. PHP array_intersect() function: PHP array_intersect() function returns
the intersection of two array. In other words, it returns the matching
elements of two array.
Example

<?php
$name1=array("sonoo","john","vivek","smith");
$name2=array("umesh","sonoo","kartik","smith");
$name3=array_intersect($name1,$name2);
foreach( $name3 as $n )
{
echo "$n<br />";
}
?>
Output:

sonoo
smith
5. PHP array_merge() Function: The array_merge() function merges one
or more arrays into one array.
<?php
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));
?>
Output:-
Array ( [0] => red [1] => green [2] => blue [3] => yellow )

You might also like