2013-05-12 40 views
1

我有一個wordpress主題下面的情況,我想在一個名爲navigation.php不能夠從PHP

$array_test = array (1,2,3,4); 

function func1() 
{ 
    global $array_test; 

    echo "test"; // to ensure that the function has been called 
    echo $array_test[2]; // this writes nothing, though the function is being called 
} 
文件中

文件來修改所謂的header.php的訪問全局數組功能func1正在調用中,仍然無法訪問$array_test[2]的值,

您有什麼想法嗎?

編輯: 我懷疑它是與WordPress或主題的問題,但我不知道 的主題是自由health_care_wp_theme

+0

它適用於Ideone ... http://ideone.com/dsTLY8 – mpyw 2013-05-12 12:19:46

+0

可能的牛逼他的兩個文件不包括在一起,沒有包含在正確的順序中,或者變量被覆蓋在其中的某處。 – scoota269 2013-05-12 12:22:01

+0

當你轉儲時''GLOBALS [2]' – 2013-05-12 12:22:04

回答

2

在navigation.php,(在情況下,如果沒有類在文件,定義了)

class Nav 
{ 
    public $array_test = array(1,2,3,4); 

    public function func1() 
    { 
     echo "test"; // to ensure that the function has been called 
     return $this->array_test[2]; 
    } 

    public function access_within_class() 
    { 
     print_r($this->array_test); 
    }  
} 

在header.php中

include 'path/to/navigation.php'; 

$nav = new Nav(); 
$the_array = $nav->func1(); 
echo $the_array; 
+0

抱歉,我不能將數組傳遞給函數,它是一個變量變量數組,我包括一個簡化的例子(這也不起作用),或者我錯過了某些東西你的解決方案 – Augustus 2013-05-12 13:15:50

+0

正確的,你應該可以訪問數組。你有沒有嘗試把所有內容都放在navigation.php的類中? – foxns7 2013-05-12 13:25:05

+0

這將工作,但我不能傳遞數組作爲參數,我需要在函數 – Augustus 2013-05-12 13:32:35