Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    May 2011
    Posts
    242
    Thanks
    1
    Thanked 57 Times in 56 Posts
    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
        
    }


  • Users who have thanked gvre for this post:

    Brady Partridge (08-17-2012)

  • #3
    New Coder
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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

  • #4
    Regular Coder
    Join Date
    May 2011
    Posts
    242
    Thanks
    1
    Thanked 57 Times in 56 Posts
    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

  • Users who have thanked gvre for this post:

    Brady Partridge (08-18-2012)

  • #5
    New Coder
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    That worked to perfection!

    Thank you so much!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •