PHP array_push()

📔 : PHP 🔗

array_push() method का use एक या एक से अधिक values को Array में append (Add) करने के लिए किया जाता है। Append होने वाली values Array के last में insert होती हैं।

PHP array_push Syntax

array_push (array $array , mixed ...$values );
Parameters
  1. array $array | required : Input Array जिसमे new values append करना चाहते हैं।

  2. mixed $value | required : $value , input value है जिसे Array में append करना है। input value mixed type की हो सकती है। आप कितनी ही values pass कर सकते हैं append करने के लिए , लेकिन कम से कम एक तो होनी ही चाहिए।

    Understanding mixed Type

    mixed type का मतलब होता है , कि आप अपनी need के according किसी भी type (String , Boolean , Array , Class , Numeric) की value pass कर सकते हैं। यह जरूरी नहीं है कि कोई special type की value ही pass करें।

  3. Return Value : value append होने के बाद Array Length return करता है।


PHP array_push Example

File : php_array_push.php

Copy Fullscreen Close Fullscreen
<?php 
  $stack = array("orange", "banana");
  echo array_push($stack, "apple", "raspberry");
  echo "<pre>";
  print_r($stack); 
  echo "</pre>";
?>
Output
4
Array
(
    [0] => orange
    [1] => banana
    [2] => apple
    [3] => raspberry
)

? array_push() function में pass किया गया पहला argument अगर Array नहीं है तो warning error आएगी।


अगर आप सिर्फ single value ही Array में Append कर रहे हैं। तो array_push() function की वजाय $array[] = use करें।

  $stack = array("orange", "banana");
  $stack[] = 'apple';
  print_r($stack);
  
 /*Output*/
 Array ( [0] => orange [1] => banana [2] => apple )

array_push() Array के last में new element insert करता है , अगर आप Array के start में element को insert करना चाहते हैं तो array_unshift() use करें।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook