PHP : Prepend a value to an array PHP

In this tutorial learn how to add value / Prepend a value to an array in php using array_unshift method



 function prependelement($array, $value)
    {
        array_unshift($array, $value);

        return $array;
    }



TUTORIAL


<?php

$array = Array
(
    '3' => 10,
    '2' => 2,
    '5' => 'Nakiya',

);
      
      
    
    /**
     * Prepend a value to an array PHP
     */
function prependelement($array, $value)
    {
        array_unshift($array, $value);

        return $array;
    }

var_dump(prependelement($array,'Jack'));
OUTPUT DEMO



array(4) {
  [0]=>
  string(4) "Jack"
  [1]=>
  int(10)
  [2]=>
  int(2)
  [3]=>
  string(6) "Nakiya"
}

No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...