Add to an array

Add to an array using array_push

<?php
   $search = array(“google”,”yahoo”,”live”,”google”,”yahoo”,”google”);
   print_r($search);
   print “<br>”;
   //lets add a couple of values and print them out again
   array_push($search,”google”,”live”);
   print_r($search);
?>

The result will be

Array ( [0] => google [1] => yahoo [2] => live [3] => google [4] => yahoo [5] => google )
Array ( [0] => google [1] => yahoo [2] => live [3] => google [4] => yahoo [5] => google [6] => google [7] => live )

1 COMMENT

  1. or if you want add new item to last index, you can use:
    $search = array(“google”,”yahoo”,”live”,”google”,”yahoo”,”google”);
    $search[] = “bing”;

    otherwise, if you want to add new item to first position in array,
    you can use array_unshift function

    $search = array(“google”,”yahoo”,”live”,”google”,”yahoo”,”google”);
    array_unshift($search,”bing”);

    //$search now array(“bing”, “google”,”yahoo”,”live”,”google”,”yahoo”,”google”);

LEAVE A REPLY

Please enter your comment!
Please enter your name here

*