简体   繁体   中英

Getting 'invalid argument for foreach' for a valid array in PHP

I'm getting the error Invalid argument supplied for foreach() even though the array being supplied appears to be a valid array. Here's my code:

print_r($keywords);
extract($product);
foreach ($keywords as $k=>$v)
{
   //stuff here
}

Here's some of the output from the print_r:

Array
(
    [0] => Array
        (
            [text] => vanguard tripod
            [language] => 
            [advertiserCompetitionScale] => 0
            [avgSearchVolume] => -1
            [lastMonthSearchVolume] => -1
        )
    .........
    [39] => Array
        (
            [text] => 63 for sale
            [language] => 
            [advertiserCompetitionScale] => 0
            [avgSearchVolume] => 6600
            [lastMonthSearchVolume] => -1
        )

)

The error I get is on the line where I do the foreach.

The array $keywords is retrieved by doing an array_merge on two similar arrays. Could that have anything to do this this?

Does $products contain an element named keywords ?

If so, print_r after the extract . extract is likely the issue here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM