2017-06-13 51 views
0

我有两个类。在我的父类中,我有这个功能:无法使用数组作为方法参数

protected function assign($items, $array) 
    { 
     foreach($items as $item) 
     { 
      $array[$item] = $item; 
     } 

     return $array; 
    } 

我使用第二个数组参数来确定要附加到哪个数组。 然后我会像这样从我的孩子班上打电话给我。

$this -> assign($this -> attributes, $this -> values); 

这并不工作,但如果我在父类中硬编码的数组名$this -> values然后它的作品,因为它应该。我想知道我做错了什么,以及如何正确地做。

感谢

+0

你的函数参数是值反,而你没有使用的返回值。 –

回答

0

试试下面的代码

protected function assign($items, &$array) 
    { 
     foreach($items as $item) 
     { 
      $array[$item] = $item; 
     } 

     return $array; 
    } 
+0

在$ array之前添加了& – Exprator