title: how to add elements to an array inside a function php
body: <p>how to add elements to a global array from inside a function if element not exist in array?</p>
  
  <p>my main code will call to function multiple times.but each time different elements will create inside the function</p>
  
  <p>my sample current code is,</p>
  
  <pre><code>$all=[];
  t(); // 1st call
  t(); //2nd call
  function t(){
  $d='2,3,3,4,4,4';  //this is a sample.but element will different for each function calling
  $d=explode(',',$d);
  foreach($d as $e){
  if(!in_array($e,$all)){
    array_push($all, $e);
         }
       }
  }
   print_r($all);
  </code></pre>
  
  <p>output is empty,</p>
  
  <pre><code>Array()
  </code></pre>
  
  <p>but i need it like this </p>
  
  <pre><code>Array
  (
      [0] =&gt; 2
      [1] =&gt; 3
      [2] =&gt; 4
  )
  </code></pre>
  
  <p>thank you</p>