2012-08-30 41 views
3

这是我的代码,当我运行这个函数时,我得到这个:Warning: array_push() expects parameter 1 to be array但是我在开始之前将$printed定义为数组。Warning:array_push()期望参数1是数组

$printed = array(); 

function dayAdvance ($startDay, $endDay, $weekType){ 
     $newdateform = array(
        'title' => date("M d", strtotime($startDay))."  to  ".date("M d", strtotime($endDay)). $type, 
        'start' => $startDay."T08:00:00Z", 
        'end' => $startDay."T16:00:00Z", 
        'url' => "http://aliahealthcareer.com/calendar/".$_GET['fetching']."/".$startDate); 

        array_push($printed, $newdateform); 

     if ($weekType=="weekend"){ 
      $days="Saturday,Sunday"; 
     } 
     if ($weekType=="day"){ 
      $days="Monday,Tuesday,Wednesday,Thuresday,Friday"; 
     } 
     if ($weekType=="evening"){ 
      $days="Monday,Tuesday,Wednesday"; 
     } 
     $start = $startDate; 
     while($startDay <= $endDay) { 
      $startDay = date('Y-m-d', strtotime($startDay. ' + 1 days')); 
      $dayWeek = date("l", strtotime($startDay)); 
      $pos = strpos($dayWeek, $days); 
      if ($pos !== false) { 
       $newdateform = array(
        'title' => date("M d", strtotime($start))."  to  ".date("M d", strtotime($endDate)). $type, 
        'start' => $startDate."T08:00:00Z", 
        'end' => $startDate."T16:00:00Z", 
        'url' => "http://aliahealthcareer.com/calendar/".$_GET['fetching']."/".$startDate); 

        array_push($printed, $newdateform); 

      } 

     } 


    } 
+2

在目前的范围,'$ printed'从未被初始化。将其声明为“全局”或将其包含在函数参数中。 – Matt

+0

[这里是变量范围的手动输入](http://php.net/manual/en/language.variables.scope.php) – Matt

回答

13

在范围中array_push()叫,$printed从未被初始化。无论是在功能的参数声明为global或包含它:

$printed = array(); 
. 
. 
. 
function dayAdvance ($startDay, $endDay, $weekType){ 
    global $printed; 
    . 
    . 
    . 
} 

OR

function dayAdvance ($startDay, $endDay, $weekType, $printed = array()) { ... } 

注:

更快替代array_push()是简单地使用附加的值传送到阵列[]

$printed[] = $newdateform; 

此方法将自动检测变量是否从未初始化,并在追加数据(换句话说,无错误)之前将其转换为数组。

UPDATE:

如果你想的$printed价值坚持的功能之外,您必须按引用传递,或声明为global。上面的例子是不等于。下面的例子就相当于使用global(并且,事实上,比使用global一个更好的做法 - 它迫使你与你的代码更加审慎,防止意外的数据操作):

function dayAdvance ($startDay, $endDay, $weekType, &$printed) { ... } 
+0

我对全球的了解已经关闭,你的第一个代码示例将其清除了,我认为你在任何地方声明了全局,而不仅仅是你需要它的地方。谢谢! – Osman

+0

请注意,不建议以这种方式使用'global'。更好的做法是通过引用传递数组。这将防止将来无意中修改数据。 – Matt

+0

降票的原因是什么?这个答案不仅是正确的,但它解释了正确使用替代方法,并推广最佳实践。 – Matt

2

您需要使用global $printed;或添加$printed作为函数参数。

您还可将$printed参数作为在功能参考:http://php.net/manual/en/language.references.pass.php

更多关于全球和可变范围:http://php.net/manual/en/language.variables.scope.php

+0

我试过这个问题,但仍然是同样的问题全局'$印; $ printed = array();' – Osman

+0

你需要在你的函数声明后添加全局变量。我在问题中添加了一个链接,以了解更多关于变量作用域和全局变量的信息。 – Manhim

+0

谢谢,我明白了! – Osman

0

相反的函数array_push()使用$your_array[] = $element_to_be_added;当你想添加一个元素到数组中。

如文档提到,这将创建一个新的数组,如果数组为空:

注:如果您使用array_push()将一个元素添加到阵列中,最好使用$阵列[ ] =因为这样就没有调用函数的开销。

和:

注意:如果第一个参数不是数组array_push()将引发警告。这与创建新数组的$ var []行为不同。

来自:http://php.net/manual/en/function.array-push.php

相关问题