Coding Villa

Push & Pop values in array using array_push, array_pop in PHP

Author : Tahreem Anwar

Share with Friends
Views : 277
Ratings : 0
Bookmarks : 0
Farourities : 0

Description


In this simple article you will learn about Push & Pop values in array using array_push, array_pop in PHP.


Advertisements


 

 

Array_push function:

 

The array_push() function Add one or more elements to the end of an array. It takes array and the one of more values to be pushed in an array.

 

Code:

 

1.    <?php

2.    $students=array("Tahreem","Rizwan","Masin","Fatima");

3.    array_push($students,"Serish","Irfan");

4.    print_r($students);

5.    echo "<br/>";

6.    ?>

 

Output:

 

Array ( [0] => Tahreem [1] => Rizwan [2] => Masin [3] => Fatima [4] => Serish [5] => Irfan )

 

Explanation:

 

On line 2 we have declared and initialize an array. On line 3 we have passed it to array_push function with the values that are to be pushed in array. Then on line 4 we have passed it to print_r that prints the entire array.

 

Array_ pop function:

 

Array_pop function deletes last element/value from an array. It takes only one parameter that is array.Array_pop(array).

 

Code:

 

1.    <?php

2.    $students=array("Tahreem","Rizwan","Masin","Fatima");

3.    array_pop($students);

4.    print_r($students);

5.    ?>

 

Output:

 

Array ( [0] => Tahreem [1] => Rizwan [2] => Masin )

 

Explanation:

 

On line 2 we have declared and initialize an array. On line 3 we have passed it to array_pop function that deletes last element of any array every time we call it. In the above code we call it one time so it deletes only one element from the last. If we call it two times it delete two elements from the last.

 

Code:

 

1.    <?php

2.    $students=array("Tahreem","Rizwan","Masin","Fatima");

3.    array_pop($students);

4.    array_pop($students);

5.    print_r($students);

6.    ?>

 

Output:

 

Array ( [0] => Tahreem [1] => Rizwan )

 

Explanation:

 

On line 2 we have declare and initialize an array. On line 3 we have passed it to array_pop function that deletes last element of any array every time we call it. In the above code we call it two times so it deletes two elements from the last. As we can see in the output.

 

 

 



Article Tags
Share with Friends

Comments




Leave a Reply

Name (required)  
Mail (will not be published) (required)   
 
Enter This Code
Captcha
 

Advertisements

Article Categories

.NET


Databases


Mobile Development


Operating Systems


Web Development


Coding Villa on Facebook