1

I'm trying to insert an array via inside a function to the specified global array (refer below)

$array = [];
function insert_array($e,$ed){
    $array[] = [ 'name' => 'array 1', 'link' => 0 ];
}
insert_array('s','d');
var_dump($array);

but does not work like the $array is still empty, any ideas, help on how to insert a value to the array inside a function?

Juliver Galleto
  • 8,831
  • 27
  • 86
  • 164
  • 5
    Possible duplicate of [Giving my function access to outside variable](https://stackoverflow.com/questions/2531221/giving-my-function-access-to-outside-variable) – Madhawa Priyashantha Oct 25 '17 at 15:59
  • 1
    Read about the [variables scope](http://php.net/manual/en/language.variables.scope.php) and the [`global`](http://php.net/manual/en/language.variables.scope.php#language.variables.scope.global) keyword then forget about global variables at all. On the long term, using global variables produces more harm than good. – axiac Oct 25 '17 at 16:09
  • 1
    Bit of an aside but why aren't you using `insert_array` arguments `$e` and `$ed`? – Darragh Enright Oct 25 '17 at 16:40

0 Answers0