Warning: array_slice() expects parameter 1 to be array, null given in

24,580

The reason it may not work is because $feed->items may not be an array(based on the fetch_rss call).

To check if something is an array, you can do:

is_array($feed->items); // returns true or false

If it is an array, you can then call array_slice()

The better solution however, is to check the $feed variable to make sure it returned what you are looking for before processing.

Share:
24,580
Bart
Author by

Bart

I am Lead R&D Engineer at the Artanim Foundation, working on (among other things) the R&D for VR/AR-related technology used in full-body interactive experiences. My Website: http://bart.kevelham.com Online Resume: http://bkevelham.github.io My LinkedIn Profile: http://ch.linkedin.com/in/bartkevelham

Updated on November 20, 2022

Comments

  • Bart
    Bart over 1 year

    I'm using the following code in my WordPress site to display the latest Youtube video from my account using rss:

    <?php
    include_once(ABSPATH.WPINC.'/rss.php'); // path to include script
    $feed = fetch_rss('http://gdata.youtube.com/feeds/api/users/UrbanGAME/uploads'); // specify feed url
    $items = array_slice($feed->items, 0, 1); // specify first and last item
    ?>
    
    <?php if (!empty($items)) : ?>
    <?php foreach ($items as $item) : ?>
    
    <object width="96%"><param name="movie" value="<?php echo str_replace("watch?v=","v/", $item['link']); ?>&hl=en_GB&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="<?php echo str_replace("watch?v=","v/", $item['link']); ?>&hl=en_GB&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="96%"></embed></object>
    
    <?php endforeach; ?>
    <?php endif; ?>
    

    It works. However, some of the times I get this error when it does not work:

     Warning: array_slice() expects parameter 1 to be array, null given in /wp-content/themes/theme1/header.php  on line 173. 
    

    Also I'm using the above code twice, for 2 spots on the site for 2 different accounts. One is in the my header.php file and the other in my footer.php file. When 1 works, the other does not, and of course I get an error/warning for the other one as well, just referring to that file and a different line. I have tried messing with the code and still nothing. Could the problem be with something in my code?

    Additional info:

    php: 5.3.2

    host: crazy domains (http://www.crazydomains.com.au/)

    apache: 2.2.15

  • Waihon Yew
    Waihon Yew about 13 years
    This will cure the symptom, not the disease.
  • Mike Lewis
    Mike Lewis about 13 years
    Added a better solution.
  • jon_darkstar
    jon_darkstar about 13 years
    by $fetch you mean $feed?
  • Mike Lewis
    Mike Lewis about 13 years
    Yes, ah I need to get some sleep :X
  • Admin
    Admin about 13 years
    I tried your suggestion and it didn't work. Instead what had happened is the rest of my template stopped working, few parts appeared and most importantly the video portion did not appear either. I do understand a bit of what you're saying. This just puzzles me as why it works about 98% of the time and then the other 2% like once every 2 weeks or now I think it has been once a week I get this warning/error message.
  • jon_darkstar
    jon_darkstar about 13 years
    then your getting the null. it doesnt solve initial problem, which is no one knows whats happening in fetch_rss