3

So i have following function :

function push_to_array()
{
    $array_of_data = func_get_arg(0);
    for($i = 1; $i < func_num_args(); ++$i){

        $array_of_data[] = func_get_arg($i);
    }
    return $array_of_data;
}

I can use it with existing array : $array = ['once','two','three'];

Function is used for pushing data into existing array.

So final call looks like :

$newArray = push_to_array($array,'haha','hoho','hihi');

Now i want to transform the call into

push_to_array($array,'haha','hoho','hihi');

so i dont need to create new variable or i dont want to, but i want to modify already existing array;

PS: i know there are functions like array_push but i want to learn it by myself.

Peter Štieber
  • 43
  • 2
  • 10
  • *"i know there are functions like array_push but i want to learn it by myself."* -- I love this. Good luck on your PHP adventures, Peter. :) – salathe Sep 18 '17 at 18:49
  • Possible duplicate of [how to pass unlimited arguments by reference in php](https://stackoverflow.com/questions/7026872/how-to-pass-unlimited-arguments-by-reference-in-php) – MaxZoom Sep 18 '17 at 19:07
  • Did you give up? – AbraCadaver Sep 29 '17 at 17:48

5 Answers5

3

You could just use the same variable:

$array = push_to_array($array,'haha','hoho','hihi');

But to answer the question, you will need to use Passing by Reference, so you must include it as an argument:

function push_to_array(&$array_of_data)
{
    for($i = 1; $i < func_num_args(); ++$i){    
        $array_of_data[] = func_get_arg($i);
    }
}

However this will achieve the same as the first example:

$array = array_merge($array,['haha','hoho','hihi']);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
1

If you're using PHP 5.6 or later, you can form variable-length functions using the ellipsis:

function push_to_array(array &$a, ...$elems) {
    foreach ($elems as $elem) {
        $a[] = $elem;
    }
}
amphetamachine
  • 27,620
  • 12
  • 60
  • 72
0

You should pass the variable by reference :

function push_to_array(&$array_of_data)
{
    for($i = 1; $i < func_num_args(); ++$i){

        $array_of_data[] = func_get_arg($i);
    }
    return $array_of_data;
}

$array = ['once','two','three'];

 push_to_array($array,'haha','hoho','hihi');

print_r($array);
Ahmad Mobaraki
  • 7,426
  • 5
  • 48
  • 69
-1

You're wanting to pass the variable as a reference.

function my_array_push(&$arr, ...$values) {
    while (($val = array_pop($values)) !== null) {
        $arr[] = $val;
    }
}

$t = [1];
my_array_push($t, [2, 3, 4]);
var_dump($t); // [1, 2, 3, 4]

I didn't see that you were aware of array_push before positing the following

For what it is worth, the functionality that you are trying to create is provided by the default array_push function.

$a = [1];

array_push($a, 2, 3, 4);

var_dump($a); // [1, 2, 3, 4];
kyle
  • 2,563
  • 6
  • 22
  • 37
-2
   function self_coded_push_to_array(&$array){
      // get all arguments
      $args = func_get_args();
      foreach($args as $i => $arg){
         // not the first argument (that's the array)
         if($i > 0){
            // add to the array
            $array[] = $arg;
         }
      }
    }


    $arr = array('donkey','monkey');

    self_coded_push_to_array($arr, 'zebra', 'whale');

    print_r($arr);

This returns:

Array (
 [0] => donkey,
 [1] => monkey,
 [2] => zebra,
 [3] => whale
)
ARN
  • 679
  • 7
  • 15