How To Use Array Push Function With Example In PHP

admin_img Posted By Bajarangi soft , Posted On 16-09-2020

using push function you can append as many values you want to the array giving one at a time and inserts one or more elements to the end of an array.

PHP array push function

Syntax for push array()

array_push(array, value1, value2, ...)
 

Parameter   Description

array         Specifies an input array
value1        Specifies the value to add 
value2        Specifies the value to add

First Need to Create Arrays
1.Create Array:
$stack = array("Orange", "Mango");

push multiple arrays into one array
<?php
$stack = array("Orange", "Mango");
array_push($stack, "Apple", "Banana");
echo '<pre>';
print_r($stack);
?>

Complete Code of array_push() Multiple Array Elements:
<!DOCTYPE html>
<html lang="en">
<head>
    <title>PHP string replace function</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
    <div class="text-center">
        <h1>How do I push multiple arrays into one array?</h1>
    </div>
    <br>
    <div class="well">
        <?php
        $stack = array("Orange", "Mango");
        array_push($stack, "Apple", "Banana");
        echo '<pre>';
        print_r($stack);
        ?>
    </div>
    <br>
</div>
</body>
</html>

Related Post