views:

261

answers:

6
    $genreList;

    function directorGen($array)
    {
     foreach($array as $value)
        {
    $genreList[] = $value; 
     }
    }

   //later..

   directorGen($title->genres());

This code results in a NULL array. If I replace $genreList[] = $value with echo $value everything prints out like expected. Any ideas?

A: 

Where is $genreList defined? It may just be a function-local variable, in which case it's lost when the function exits. If it's a class-level variable, remember to use $this->genreList instead.

Edit

My mistake. If it's a global variable, you need to add this to the top of the function in order for PHP to find it:

global $genreList;
Turnor
This isn't OO code.
yes we know it's not OO code, but scope of variable/array (global/local) was basic of PHP, not only related with OO ut the whole concept of PHP...
Dels
A: 

It's a scope issue. The $genreList within directorGen() only exists within directorGen(), it's not implicitly a global just because it was mentioned outside the function. Try using global $genreList at the top of the function.

Adam Backstrom
Nevermind.. gotta put it in the function! Thanks.
Funny, considering your accepted answer added a global declaration to the top of the function.
Adam Backstrom
A: 

Either of the following should fix your problem;

$genreList[] .= $value; // Appends each value to the array. 

array_push($genreList, $value);

Also my mistake, you should be returning the genreList from the function via this statement:

return $genreList;

Using the global keyword is generally considered a code smell as it can create numerous issues in tracking the usage of the global variable as well as its value as it changes.

Noah Goodrich
The string concatenation operator in your first line of code is redundant.
Adam Backstrom
+2  A: 

If $genreList is a global variable, there's your problem: it's a scope issue. It can easily be fixed with:

$genreList = array();

function directorGen($array) {
    global $genreList;
    foreach($array as $value) {
        $genreList[] = $value;        
    }
}

Note: while not strictly necessary I also initialized it, which I think is good practice.

If directorGen() is a member function and $genreList is a data member then change to:

function directorGen($array) {
    foreach($array as $value) {
        $this->genreList[] = $value;        
    }
}
cletus
A: 
function directorGen($array)
{
    $genreList = array();

    foreach($array as $value)
    {
        $genreList[] = $value;        
    }

    return $genreList;
}

//later..

directorGen($title->genres());

You will always receive array. Even it is empty. And you not need to check it with is_array() function.

Zahar
A: 

This is probably what you want to do:

class Test {
    //public, protected, private ...
    var $genreList = array();

    function directorGen(array $array) {
     //remove string keys
     $values = array_values($array);
     $this->genreList = array_merge($this->genreList, $values);
    }
}

NB: Will reset the counters. If you unset a value and then add new values, the keys will be reset with lowest at 0 and max at count - 1.

OIS