Need help! Warning: Invalid argument supplied for foreach()

Jello, First time poster.

I need some help fixing this problem, my coding experience in PHP is limited so go easy on me.
I am getting this error: Warning: Invalid argument supplied for foreach() … on line 334
Here is the code the error is on line 6 below … foreach ($ids as $id)

[php]function getWidgets($position = null) {

if (empty($this->widgets)) {
    foreach (wp_get_sidebars_widgets() as $pos => $ids) {
        $this->widgets[$pos] = array();
                foreach ($ids as $id)                                                     <----- this is the line
                        {
            $this->widgets[$pos][$id] = $this->getWidget($id);
        }
    }
}

if (!is_null($position)) {
    return isset($this->widgets[$position]) ? $this->widgets[$position] : array();
}

return $this->widgets;

}[/php]

Any help would be greatly appreciated. And I will dole out the Karma.

Hi,

Are you confident the $ids is an array at the point you feed it to a foreach?

What happens if you put the next line just above the line that causes your grief:
[php]
echo '$ids is '. ( is_array( $ids ) ? ‘an array!’ : ‘not an array.’ ). “
\n”;
[/php]

if it doesn’t spit out ‘[tt]$ids is an array![/tt]’ you know why you get the error.
If you want to research the structure of the $ids variable or the $pos variable, you can use [tt]var_dump()[/tt].

I hope this helps,
O :smiley:

Sponsor our Newsletter | Privacy Policy | Terms of Service