Array
Function
|
What it Does
|
print_r()
|
Print the value and index of an array(in
assciative form)
|
<?php
str=array("welcome","student"); print_r($str); ?> Output : Array([0]=>"welcome",[1]=>"student") |
|
explode()
|
Splits a string into array elements
|
<?php
$str="welcome user how r you"; //print_r($str); $arr=explode(" ",$str); print_r($arr); //explode $val="10,20,30,40,50,60"; $arr1=explode(",",$val); foreach($arr1 as $v) { @$s+=$v; } echo $s; ?> Output : Array([0]=>"welcome",[1]=>"student") |
|
implode()
|
Joins array elements into a string
|
<?php
$arr=array("hello","user"); $str=implode(" ",$arr); echo $str; ?> Output : hello user |
|
count()
|
Returns an integer value indicating how many
elements an array contains
|
<?php
$arr=array(10,20,30,40,50); echo "Number of elements= ".count($arr)."<br/>"; //use inside for loop to check condition for($i=0;$i<count($arr);$i++) { @$sum=$sum+$i; } echo "sum=".$sum; ?> Output : Number of elements=5 sum=150 |
|
range()
|
Generates a number range as an array
|
<?php
$range=range("A","M"); print_r($range); ?> Output : Array ( [0] => A [1] => B [2] => C [3] => D [4] => E [5] => F [6] => G [7] => H [8] => I [9] => J [10] => K [11] => L [12] => M ) |
|
max()
|
Finds the largest value in an array
|
<?php
$max=array(10,20,30,40,100,50); echo max($max); ?> Output : 100 |
|
min()
|
Finds the smallest value in an array
|
<?php
$max=array(10,20,30,40,100,50); echo min($max); ?> Output : 10 |
|
shuffle()
|
Randomly rearranges the sequence of elements
in an array
|
<?php
$arr=range("a","z"); shuffle($arr); print_r($arr); ?> Output : Refresh page, on every refresh it rearrange the index of array. Array ( [0] => g [1] => a [2] => c [3] => d [4] => b [5] => k [6] => i [7] => l [8] => f [9] => j [10] => e [11] => m [12] => h ) |
|
array_rand()
|
Finds the random index of an array
|
<?php
$arr=range("a","z"); $rand=array_rand($arr); echo $rand; ?> Output : Refresh page, on every refresh it shows an random index b/w a to z. |
|
array_slice()
|
Extracts a segment of an array
|
<?php
$color=array("blue","red","green","pink"); $slice=array_slice($color,1,2); print_r($slice); ?> Output : Array ( [0] => red [1] => green ) |
|
array_shift()
|
Removes an element from the beginning of an
array
|
<?php
$city=array("delhi","patna","noida"); array_shift($city); print_r($city); ?> Output : Array ( [0] => patna [1] => noida ) |
|
array_unshift()
|
Adds an element to the beginning of an array
|
<?php
$city=array("delhi","patna","noida"); array_unshift($city,"banglore"); print_r($city); ?> Output : Array ( [0] => banglore [1] => delhi [2] => patna [3] => noida ) |
|
array_pop()
|
Removes an element from the end of an array
|
<?php
$city=array("delhi","patna","noida"); array_pop($city); print_r($city); ?> Output : Array ( [0] => delhi [1] => patna ) |
|
array_push()
|
Adds an element to the end of an array
|
<?php
$city=array("delhi","patna","noida"); array_push($city,"gurgaon"); print_r($city); ?> Output : Array ( [0] => delhi [1] => patna [2] => noida [3] => gurgaon ) |
|
array_unique()
|
Removes duplicate elements from an array
|
<?php
$city=array("delhi","patna","noida","delhi","patna"); $uni=array_unique($city); print_r($uni); ?> Output : Array ( [0] => delhi [1] => patna [2] => noida ) |
|
array_reverse()
|
Reverses the sequence of elements in an array
|
<?php
$city=array("delhi","patna","noida"); $rev=array_reverse($city); print_r($rev); ?> Output : Array ( [0] => noida [1] => patna [2] => delhi ) |
|
array_merge()
|
Combines two or more arrays
|
<?php
$city=array("delhi","patna"); $city1=array("gurgaon","banglore"); $merg=array_merge($city,$city); print_r($merg); ?> Output : Array ( [0] => delhi [1] => patna [2] => gurgaon [3] => banglore) |
|
array_intersect()
|
Calculates the common elements between two or
more arrays
|
<?php
$arr1=array("red","blue","pink"); $arr2=array("black","blue","white"); $common=array_intersect($arr1,$arr2); print_r($common); ?> Output : Array ( [1] => blue ) |
|
array_diff()
|
Calculates the difference between two arrays
|
<?php
$arr1=array("red","blue","pink"); $arr2=array("black","blue","white"); $diff=array_diff($arr2,$arr1); print_r($diff); ?> Output : Array ( [0] => black [2] => white ) |
|
in_array()
|
Checks if a particular value exists in an
array
|
<?php
$arr1=array("red","blue","pink"); echo in_array("red",$arr1); ?> Output : 1 |
|
array_key_exists()
|
Checks if a particular key exists in an array
|
<?php
$arr1=array("red","blue","pink"); echo array_key_exists(1,$arr1); ?> Output : 1 |
|
sort()
|
Sorts an array
|
<?php
$arr=array(100,11,5,12,150,0); sort($arr); print_r($arr); ?> Output : Array ( [0] => 0 [1] => 5 [2] => 11 [3] => 12 [4] => 100 [5] => 150 ) |
|
rsort()
|
Reverse Sorts an array
|
<?php
$arr=array(100,11,5,12,150,0); rsort($arr); print_r($arr); ?> Output : Array ( [0] => 150 [1] => 100 [2] => 12 [3] => 11 [4] => 5 [5] => 0 ) |
|
asort()
|
Sorts an associative array by value
|
<?php
$col=array("a"=>"red","z"=>"blue","y"=>"green"); asort($col); print_r($col); ?> Output : Array ( [z] => blue [y] => green [a] => red ) |
|
arsort()
|
Reverse-Sorts an associative array by value
|
<?php
$col=array("a"=>"red","z"=>"blue","y"=>"green"); arsort($col); print_r($col); ?> Output : Array ( [a] => red [y] => green [z] => blue ) |
|
ksort()
|
Sorts an associative array by key(index)
|
<?php
$col=array("a"=>"red","z"=>"blue","y"=>"green"); ksort($col); print_r($col); ?> Output : Array ( [a] => red [y] => green [z] => blue ) |
|
krsort()
|
Reverse-Sorts an associative array by
key(index)
|
<?php
$col=array("a"=>"red","z"=>"blue","y"=>"green"); krsort($col); print_r($col); ?> Output : Array ( [z] => blue [y] => green [a] => red ) |
|
array_product()
|
Calculates the product of an array.
|
<?php
$a=array(5,5,5,4); echo(array_product($a)); ?> Output : 500 |
|
array_sum()
|
Calculates the sum of an array.
|
<?php
$a=array(0=>5,1=>15,2=>25,15); echo array_sum($a); ?> Output : 60 |
|
array_search()
|
Search an array for a value and returns the
key
|
<?php
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse"); echo array_search("Horse",$a); ?> Output : c |
|
array_splice()
|
Removes selected elements from an array and
replaces it with new elements.It also returns an array with the removed
elements.
|
<?php
$a1=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird"); $a2=array(0=>"Tiger",1=>"Lion"); print_r(array_splice($a1,0,2,$a2)); ?> Output : Array ( [0] => Dog [1] => Cat ) |
|
current()
|
Returns the value of the current element in an
array.It returns FALSE on empty elements or elements with no value
|
<?php
$arr=array("dog","cat","horse"); echo current($arr); ?> Output : dog |
|
key()
|
Fetch a key from an array
|
<?php
$array = array( 'fruit1' => 'apple', 'fruit2' => 'orange', 'fruit3' => 'grape', 'fruit4' => 'apple', 'fruit5' => 'apple'); while ($fruit_name = current($array)) { if ($fruit_name == 'apple') { echo key($array).'
';
} next($array); } ?> Output : fruit1 fruit4 fruit5 |
No comments:
Post a Comment