我的PHP数组出了什么问题?

时间:2021-03-23 15:40:42
    $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?

此代码导致NULL数组。如果我用echo $ value替换$ genreList [] = $ value,那么所有内容都会像预期的那样打印出来。有任何想法吗?

6 个解决方案

#1


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

如果$ genreList是一个全局变量,那就是你的问题:这是一个范围问题。它可以很容易地修复:

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

如果directorGen()是一个成员函数而$ genreList是一个数据成员,那么改为:

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

#2


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.

$ genreList在哪里定义?它可能只是一个函数局部变量,在这种情况下它会在函数退出时丢失。如果它是类级变量,请记住使用$ this-> genreList。

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:

我的错。如果它是一个全局变量,你需要将它添加到函数的顶部,以便PHP找到它:

global $genreList;

#3


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.

这是一个范围问题。 directorGen()中的$ genreList只存在于directorGen()中,它不是隐式的全局因为它是在函数外部提到的。尝试在函数顶部使用全局$ genreList。

#4


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:

还有我的错误,你应该通过这个语句从函数返回genreList:

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.

使用global关键字通常被认为是代码气味,因为它可以在跟踪全局变量的使用及其变化时的值时产生许多问题。

#5


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.

你将永远收到数组。即使它是空的。而且您不需要使用is_array()函数进行检查。

#6


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.

注意:将重置计数器。如果取消设置值然后添加新值,则键将重置为最低值为0,最大值为count - 1。

#1


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

如果$ genreList是一个全局变量,那就是你的问题:这是一个范围问题。它可以很容易地修复:

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

如果directorGen()是一个成员函数而$ genreList是一个数据成员,那么改为:

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

#2


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.

$ genreList在哪里定义?它可能只是一个函数局部变量,在这种情况下它会在函数退出时丢失。如果它是类级变量,请记住使用$ this-> genreList。

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:

我的错。如果它是一个全局变量,你需要将它添加到函数的顶部,以便PHP找到它:

global $genreList;

#3


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.

这是一个范围问题。 directorGen()中的$ genreList只存在于directorGen()中,它不是隐式的全局因为它是在函数外部提到的。尝试在函数顶部使用全局$ genreList。

#4


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:

还有我的错误,你应该通过这个语句从函数返回genreList:

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.

使用global关键字通常被认为是代码气味,因为它可以在跟踪全局变量的使用及其变化时的值时产生许多问题。

#5


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.

你将永远收到数组。即使它是空的。而且您不需要使用is_array()函数进行检查。

#6


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.

注意:将重置计数器。如果取消设置值然后添加新值,则键将重置为最低值为0,最大值为count - 1。