A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearWhat's wrong with my PHP array? - page 1
User InfoPosts
What's wrong with my PHP array?#1
    $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?

posted date: 2009-04-12 19:21:00


Re: What's wrong with my PHP array?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-12 19:21:01


Re: What's wrong with my PHP array?#3
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.EditMy 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;

posted date: 2009-04-12 19:24:00


Re: What's wrong with my PHP array?#4
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.

posted date: 2009-04-12 19:25:00


Re: What's wrong with my PHP array?#5
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.

posted date: 2009-04-12 19:27:00


Re: What's wrong with my PHP array?#6
This isn't OO code.

posted date: 2009-04-12 19:28:00


Re: What's wrong with my PHP array?#7
Nevermind.. gotta put it in the function! Thanks.

posted date: 2009-04-12 19:32:00


Re: What's wrong with my PHP array?#8
The string concatenation operator in your first line of code is redundant.

posted date: 2009-04-12 19:33:00


Re: What's wrong with my PHP array?#9
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.

posted date: 2009-04-13 03:46:00


Re: What's wrong with my PHP array?#10
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.

posted date: 2009-04-13 08:35:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.