php 警告:array_push() 期望参数 1 是数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12204713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning: array_push() expects parameter 1 to be array
提问by Osman
This is my code, and when I run this function I get this :Warning: array_push() expects parameter 1 to be arrayHowever I define $printedas an array prior to starting.
这是我的代码,当我运行这个函数时,我得到了这个:Warning: array_push() expects parameter 1 to be array但是我$printed在开始之前定义为一个数组。
$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);
}
}
}
回答by Matt
In the scope in which array_push()is called, $printedwas never initialized. Either declare it as globalor include it in the function parameters:
在array_push()被调用的范围内,$printed从未被初始化。将其声明为global或包含在函数参数中:
$printed = array();
.
.
.
function dayAdvance ($startDay, $endDay, $weekType){
global $printed;
.
.
.
}
OR
或者
function dayAdvance ($startDay, $endDay, $weekType, $printed = array()) { ... }
NOTE:
笔记:
A faster alternative to array_push()is to simply append values to your array using []:
一种更快的替代方法array_push()是使用以下方法简单地将值附加到数组中[]:
$printed[] = $newdateform;
This method will automatically detect if the variable was never initialized, and convert it to an array prior to appending the data (in other words, no error).
此方法将自动检测变量是否从未初始化,并在附加数据之前将其转换为数组(换句话说,没有错误)。
UPDATE:
更新:
If you want the value of $printedto persist outside of the function, you must either pass it by reference or declare it as global. The above examples are NOTequivalent. The following example would be equivalent to using global(and is, in fact, a better practice than using global- it forces you to be more deliberate with your code, preventing accidental data manipulation):
如果您希望 的值$printed保留在函数之外,则必须通过引用传递它或将其声明为global。上面的例子不是等价的。下面的示例相当于 using global(实际上,这是比 using 更好的实践global- 它迫使您更加谨慎地处理代码,防止意外的数据操作):
function dayAdvance ($startDay, $endDay, $weekType, &$printed) { ... }
回答by maaudet
You need to use global $printed;or to add $printedas a function parameter.
您需要使用global $printed;或添加$printed作为函数参数。
You may also pass the $printedparameter as reference in your function: http://php.net/manual/en/language.references.pass.php
您也可以$printed在函数中将参数作为参考传递:http: //php.net/manual/en/language.references.pass.php
More about global and variable scopes: http://php.net/manual/en/language.variables.scope.php
有关全局和变量范围的更多信息:http: //php.net/manual/en/language.variables.scope.php
回答by trinity420
Instead of the function array_push()use $your_array[] = $element_to_be_added;when you want to add a single element to the array.
当您想将单个元素添加到数组时,请不要array_push()使用函数$your_array[] = $element_to_be_added;。
As mentioned in the docs, this creates a new array, if the array is null:
如文档中所述,如果数组为空,则会创建一个新数组:
Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.
注意:如果您使用 array_push() 向数组添加一个元素,最好使用 $array[] = ,因为这样就没有调用函数的开销。
and:
和:
Note: array_push() will raise a warning if the first argument is not an array. This differs from the $var[] behaviour where a new array is created.
注意:如果第一个参数不是数组,array_push() 将引发警告。这与创建新数组的 $var[] 行为不同。
回答by Jhonny Agudelo
You need validate whit is_array:
你需要用 is_array 验证:
Example
例子
if (is_array($arNumbers)) {
$cellid = array_push($arNumbers, 0);
}

