...

View Full Version : Function Array and Foreach Errors



Brady Partridge
08-17-2012, 07:56 AM
Sorry if I haven't worded the post title properly but I know absolutely nothing about php.

I visited a blog I'm setting up today, only to find two php errors at the top of the homepage.


Warning: array_diff() [function.array-diff]: Argument #2 is not an array in ...


Warning: Invalid argument supplied for foreach() in ...

This first error is on line 719 of the code:


$permanent_deleted_posts = array_diff($all_products,$all_posts);

The second error is on line 725:


foreach ($permanent_deleted_posts as $permanent_deleted_post)


How do I fix these errors?

Many thanks for your help!

BLOG (http://latesthdtvreviews.com/)

gvre
08-17-2012, 08:08 AM
You should add is_array (http://php.net/manual/en/function.is-array.php) check before array_diff and foreach. e.g.

if (is_array($permanent_deleted_posts))
{
foreach ($permanent_deleted_posts as $permanent_deleted_post)
{
// do something here
}
}

Brady Partridge
08-17-2012, 01:15 PM
Thanks for your response.

I'm not sure if I read your instructions right. Do you mean I should add the code like this?


if (is_array($permanent_deleted_posts))
$permanent_deleted_posts = array_diff($all_products,$all_posts);

And this?


if (is_array($permanent_deleted_posts))
foreach ($permanent_deleted_posts as $permanent_deleted_post)

Am I supposed to add this bit of code too?


// do something here

gvre
08-17-2012, 01:25 PM
if (is_array($all_products) && is_array($all_posts))
$permanent_deleted_posts = array_diff($all_products, $all_posts);


if (is_array($permanent_deleted_posts))
foreach ($permanent_deleted_posts as $permanent_deleted_post)

Brady Partridge
08-18-2012, 04:07 AM
That worked to perfection!

Thank you so much!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum