Header Ads

Array Functionality With PHP

An array stores multiple values in one single variable:

Array functions are:count(), sort(), rsort(), asort(), ksort(), arsort(), krsort().



Array: 
An array is collection of homogeneous elements or Collection Of Similar Data Type.An array holds several values under an individual name, and you may access the actual value by referring to an index number.


There Are Following Array Functions.



Merge Array:


Merge Array Are Use For Merging One or More Array Into an One Array as you can See in Video.

<?php

$cars=array("BMW","Toyota","Maruti","Buggatti");

$bikes=array("BMW","Honda","Bajaj","Yamaha");

$show=array_merge($cars,$bikes);

print_r($show);

?>


MultiSort Array:

array_multisort() can be used to sort several arrays at once, or a multi-dimensional array by one or more dimensions.

<?php

$cars=array("BMW","Toyota","Maruti","Buggatti");

array_multisort($cars);

print_r($cars);

?>

Array POP:

pops and returns the last value of the array, shortening the array by one element

<?php

$a=array("Yellow","Green","Blue","Red");

array_pop($a);

print_r($a);

?>

Array PUSH:

Array_Push Function is Used to Add One or More Element onto the end Of Array.

<?php

$a=array("Red","Green","White","Orange","Cyan");

array_push($a,"Blue","Yellow");

print_r($a);

?>

Array Reverse:

The array_reverse() function is used to reverse the order of the elements in an array.

<?php

$a=array("A"=>"Apple","B"=>"Ball","C"=>"CAT","D"=>"Doggie");

print_r(array_reverse($a));

?>


Array Unique:

Removes duplicate values from an array.

<?php

$a=array("A"=>"Apple","B"=>"Ball","C"=>"CAT","D"=>"Doggie","B"=>"Ball");

print_r(array_unique($a));

?>


Asort Array:

The asort() function takes an array and sorts it by its values while preserving the keys.
The asort() function is used to sort associative array. It is the associative array version of sort().


<?php

$age=array("Peter"=>"35","Parker"=>"30","Rahul"=>"28");

asort($age);

foreach($age as $x=>$x_value){

echo "Name ".$x.", Age= ".$x_value;

echo"<br>";

}

?>



Count Array:

The count() function can be used to count the number of items in an array.

<?php

$cars=array("BMW","Toyota","Maruti","Buggatti");

echo count($cars);

?>


KRSort Array:

The krsort() function sorts an associative array in descending order, according to the key.

<?php

$age=array("Deepak"=>"21", "Ankit"=>"20", "Rahul"=>"25", "Sam"=>"18");

krsort($age);

foreach($age as $x=>$x_value){

echo "Name ".$x.", Age= ".$x_value;

echo"<br>";

}

?>

KSort Array:

The ksort() function sorts an associative array in ascending order, according to the key.


<?php

$age=array("Deepak"=>"21","Ankit"=>"20","Rahul"=>"25","Sam"=>"23");

ksort($age);

foreach($age as $x=>$x_value){

echo "Name ".$x.", Age= ".$x_value;

echo"<br>";

}

?>

Rsort Array:


The rsort() function sorts an array in descending order.


<?php

$cars=array("BMW","Toyota","Mclaren","Ferrari","Dodge");

rsort($cars);

$lan=count($cars);

for($x=0;$x<$lan;$x++){

echo $cars[$x];

echo"<br>";

}

?>

No comments

Powered by Blogger.