2013-05-12 63 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