Most Useful Array Functions in PHP


 

 Array functions in PHP

 


In this blog I am going to teaching you some of the most useful and frequently used array functions. Array functions in PHP, generally used to manipulate arrays. These are inbuilt in PHP no need to download any other  software. Here you will find some array functions list in the down below. We discuss each and every function in detail with syntax and examples.

 

Top 10 useful Array functions in PHP

 

  1.  count
  2.  sizeof
  3.  array_sum
  4.  array_pop
  5.  array_push
  6.  array_rand
  7.  unset
  8.  in_array
  9.  sort
  10.  rsort

 

TopMenu↑

1. Count function

 
 
 
count: prints count of total no. of elements in an array. 
 
Syntax:
 
 
count($array-variable); 
 
example:
 
<?php
 
$cart= array("pen", "notebook", "eraser");
 
$n=count($cart);
 
echo " $n Items in the Cart.<br>";
 
for($i=0 ; $i<$n ; $i++)
{
echo "Item" . $i . "=". $cart[$i] . "<br>"
}
 
?>

 output

3 Items in the Cart.
Item0=pen
Item1=notebook
Item2=eraser 

MenuTop↑

 

2. Sizeof function

 

sizeof: will returns the total array elements available in an array. 
It is similar to count function

 

example: 

<?php 
 
$names = array("John", "Jenny", "Sony", "Anna");
 
$array_size=sizeof($names);
 
print "<br>Array size = ". $array_size;
 
?> 
 
output
Array size = 4 
 

GoTop↑

 
 

3. Array_sum function

 

array_sum:  sums up all the array elements and returns the total value.
 
 
<?php

$score= array(56,76,89);
 
$total = array_sum($score);
 
print_r($score);

print "<br> Total Score: ". $total;

?>
 
 
Array ( [0] => 56 [1] => 76 [2] => 89 )
 
Total Score: 221
 
 
 
 
 

MoveTop↑

 
 
 

4. Array_pop function

 

array_pop: removes and returns last element of an array
 
example: 
 
<?php 
 
$names = array("John", "Jenny", "Sony", "Anna"); 
 
$name=array_pop($names);
 
print "<br>Removed element is :". $name. "<br>" ;
 
print_r($names);
 
print "<br>"
 
?>  
 
output
 
Removed element=Anna
 
Array ( [0] => John [1] => Jenny [2] => Sony)
 

GotoTop↑

 
 

5. Array_push function

  
array_push : inserts an element to an array
 
example1: 
 
<?php  
 
$names = array("John", "Jenny", "Sony", "Anna");  
 
array_push ($names,"Steave");
 
print_r($names);
 
print "<br>"
 
print_r($names); 
 
?>  
 
output
Array ( [0] => John [1] => Jenny [2] => Sony [3] => Anna )
Array ( [0] => John [1] => Jenny [2] => Sony [3] => Anna [4] => Steve )


 
example2:
 
<?php  
 
$pets=array("Dog", "Cat", "Bird");
 
print_r($pets);
 
print "<br>";
 
array_push($pets, "Horse", "Rabit");
 
print_r($pets);
 
print "<br>"
 
?>  
 
output:
 
Array ( [0] => Dog [1] => Cat [2] => Bird )
Array ( [0] => Dog [1] => Cat [2] => Bird [3] => Horse [4] => Rabbit )
 

 

GoTop↑

6. Array_rand function

 
 
array_rand: prints random value from an array i.e., It shuffles the array  
and gives one key. 
 
 
 
example: 
 
<?php  
 
$colors = array(1 => "red", 2 => "green", 3 => "blue", 4 => "white", 5 => "yellow" );
print_r($colors);
$random_key = array_rand($colors);
 
print "<br>key = ".$random_key;
print "<br>value = ".$colors [$random_key] ;
print "<br>";
 
 ?> 
 
output
Array ( [1] => red [2] => green [3] => blue [4] => white [5] => yellow )
key = 4
value = white 
 

Top↑

 
 

7. In_array function

 
 
in_array(): To check wether the given element is in the array or not
 
example: 

<?php

$browsers = array("Mozilla", "Chrome", "Edge", "Safari");
 
if (in_array("Edge", $browsers )) 
{
    echo "Got Edge";
} 
 
 echo "<br>"; 
 print_r($browsers );

?>
 
output
Got Edge
Array ( [0] => Mozilla [1] => Chrome [2] => Edge [3] => Safari ) 

Top↑

 

8. Unset function

 
unset(): function removes or unset the defined variable in the PHP script.
 
 
example:

 

<?php


$cart = array("pen", "notebook", "eraser");
 
unset($cart[1]); 
 
print_r($cart);

?>

output

Array ( [0] => pen [2] => eraser )

 

GotoMenu↑

9. Sort function

 

Sort: sorts array elements in ascending order.
 
example: 
  
<?php
 
$names = array("John", "Sony", "Henry", "Anna");
 
print('<br> Array <br>');
 
foreach($names as $i){ // John Sony Henry Anna
 
    print $i . " "
 
}

  
print('<br><br />sorted Array: <br>');
 
sort($names); 
 
foreach($names as $i){ // Anna Henry John Sony
 
    print $i . " ";
}
 
?> 
output
 
Array
John Sony Henry Anna

sorted Array:
Anna Henry John Sony 
 

Top↑

 

10. Rsort function

 
 
rsort: reverse sort, sorts an array in reverse or descending order. 
 
exapmple:
 
<?php
 
$names = array("Jenny", "Sony", "Henry", "Anna");
 
print('<br><br />Reverse sort :<br>');
 
 
rsort($names); 
 
foreach($names as $i){ // Sony Jenny Henry Anna
     
    print $i . " "
}
 
print "<br>";
 
?>

output

Reverse sort :
Sony Jenny Henry Anna

 NEXT

 

 

 Top↑

 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP