11

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 prior to starting.

$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
  • 2
    In the current scope, $printed was never initialized. Either declare it as global or include it in the function parameters. Commented Aug 30, 2012 at 20:11
  • Here's the manual entry on variable scope Commented Aug 30, 2012 at 20:17

4 Answers 4

28

In the scope in which array_push() is called, $printed was never initialized. Either declare it as global or include it in the function parameters:

$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 []:

$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 $printed to persist outside of the function, you must either pass it by reference or declare it as global. The above examples are NOT equivalent. 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):

function dayAdvance ($startDay, $endDay, $weekType, &$printed) { ... }
Sign up to request clarification or add additional context in comments.

4 Comments

My understanding of global was off, your first code example cleared it up, I thought you declare global anywhere and not just where you need it. THANKS!
Please note that it's not recommended to use global in this fashion. The better practice is to pass the array by reference. This will prevent unintentionally modifying the data in the future.
Reason for down-vote? Not only is this answer correct, but it explains proper use of alternative methods, and promotes best practices.
References are bad in PHP and should be avoided when possible. I would rather just prefer to return the $printed variable. (Didn't dv though)
3

You need to use global $printed; or to add $printed as a function parameter.

You may also pass the $printed parameter as reference in your function: http://php.net/manual/en/language.references.pass.php

More about global and variable scopes: http://php.net/manual/en/language.variables.scope.php

2 Comments

I tried this and still same issue global $printed; $printed = array();
You need to add global after your function declaration. I have added a link in my questions to know more about variable scopes and globals.
0

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

Comments

0

You need validate whit is_array:

Example

if (is_array($arNumbers)) {
    $cellid =  array_push($arNumbers, 0);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.