Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-17-2012, 07:56 AM   PM User | #1
Brady Partridge
New Coder

 
Join Date: Jun 2012
Location: Australia
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Brady Partridge is an unknown quantity at this point
Function Array and Foreach Errors

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.

Code:
Warning: array_diff() [function.array-diff]: Argument #2 is not an array in ...
Code:
Warning: Invalid argument supplied for foreach() in ...
This first error is on line 719 of the code:

Code:
$permanent_deleted_posts = array_diff($all_products,$all_posts);
The second error is on line 725:

Code:
foreach ($permanent_deleted_posts as $permanent_deleted_post)
How do I fix these errors?

Many thanks for your help!

BLOG
Brady Partridge is offline   Reply With Quote
Old 08-17-2012, 08:08 AM   PM User | #2
gvre
Regular Coder

 
Join Date: May 2011
Posts: 212
Thanks: 1
Thanked 50 Times in 49 Posts
gvre is an unknown quantity at this point
You should add is_array check before array_diff and foreach. e.g.
PHP Code:
if (is_array($permanent_deleted_posts))
{
    foreach (
$permanent_deleted_posts as $permanent_deleted_post)
    {
         
// do something here
    
}

gvre is offline   Reply With Quote
Users who have thanked gvre for this post:
Brady Partridge (08-17-2012)
Old 08-17-2012, 01:15 PM   PM User | #3
Brady Partridge
New Coder

 
Join Date: Jun 2012
Location: Australia
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Brady Partridge is an unknown quantity at this point
Thanks for your response.

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

Code:
if (is_array($permanent_deleted_posts))
$permanent_deleted_posts = array_diff($all_products,$all_posts);
And this?

Code:
if (is_array($permanent_deleted_posts)) 
foreach ($permanent_deleted_posts as $permanent_deleted_post)
Am I supposed to add this bit of code too?

Code:
// do something here
Brady Partridge is offline   Reply With Quote
Old 08-17-2012, 01:25 PM   PM User | #4
gvre
Regular Coder

 
Join Date: May 2011
Posts: 212
Thanks: 1
Thanked 50 Times in 49 Posts
gvre is an unknown quantity at this point
PHP Code:
if (is_array($all_products) && is_array($all_posts))
    
$permanent_deleted_posts array_diff($all_products$all_posts); 
PHP Code:
if (is_array($permanent_deleted_posts)) 
    foreach (
$permanent_deleted_posts as $permanent_deleted_post
gvre is offline   Reply With Quote
Users who have thanked gvre for this post:
Brady Partridge (08-18-2012)
Old 08-18-2012, 04:07 AM   PM User | #5
Brady Partridge
New Coder

 
Join Date: Jun 2012
Location: Australia
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Brady Partridge is an unknown quantity at this point
That worked to perfection!

Thank you so much!
Brady Partridge is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:50 AM.


Advertisement
Log in to turn off these ads.