How to Delete first or last index and value of an array using PHP

If you want to delete first or last element of array .Then you can use two very useful php functions

1- array_shift() --- It delete the first index and value of an array.
2- array_pop() --- It delete the last index and value of an array.

Example Of uses :-

My array is like below :- 

$country =array("0"=>"america" ,  "1"=>"india" ,  "2"=>"france") ;

I want to remove first element (america) from my array 

array_shift($country);

print_r($country);

Output :  array( "1"=>"india" ,  "2"=>"france") ;

Same as it is now i want to remove last element (france) from my array

array_pop($country);

print_r($country);

Output :  array("0"=>"america" ,  "1"=>"india") ;


If you like this post please don't forgot to notify me to post a comment .

Chears
Happy coding guys :)




2 Comments

Previous Post Next Post