2017-04-02 83 views
-2

我想聲明一個全局數組,並將數據放入一個函數中。在PHP中聲明全局數組

它的工作原理只有在全局變量是一個字符串,但如果它是一個數組:

$data = iterate("USER361"); 
print_r($data); 

function iterate($username) 
{ 
    global $gv_array; 

    $sql = "select * from account where sponsor = '$username'"; 
    $result = mysql_query($sql); 
     while($row = mysql_fetch_array($result)) 
     { 
      $this_user = $row['username']; 
      $this_name = $row['full_name']; 
      $this_tier = $row['tier']; 

      $my_array = array("username"=>$this_user,"name"=>$this_name,"tier"=>$this_tier); 
      array_push($gv_array[],$my_array); 

      iterate($row['username']); 
     } 

}//end of iterate 

我得到的錯誤是:

PHP的警告:array_push()預計參數1是數組,在/var/www/html/inc/code.php空給出線29

+2

的可能的複製[PHP如何在全球範圍內訪問該陣列?(http://stackoverflow.com/questions/11743275/php-how-to-access-this-array-globally) – julekgwa

+1

不應該這行'array_push($ gv_array [],$ my_array);'是'array_push($ gv_array,$ my_array);' –

+0

檢查在這裏:http://stackoverflow.com/questions/12876222/declaring-a-global-array –

回答

0

如果$gv_array它是一個數組,那麼問題就在這裏array_push($gv_array[],$my_array); REM在方括號內。

array_push($gv_array,$my_array);