Warning: array_push() expects parameter 1 to be array

前端 未结 4 993
北海茫月
北海茫月 2021-02-06 06:17

This is my code, and when I run this function I get this :Warning: array_push() expects parameter 1 to be array However I define $printed as an array

4条回答
  •  忘掉有多难
    2021-02-06 06:58

    Instead of the function array_push() use $your_array[] = $element_to_be_added; when you want to add a single element to the array.

    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.

    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.

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

提交回复
热议问题