emilyfrazier Posted December 12, 2006 Share Posted December 12, 2006 Hi,I am getting an error about array push, but I've declared my global variables. The error I get is:[color=red]warning: array_push() [function.array-push]: First argument should be an array in /home/retnkes9/public_html/drupal/themes/retn/page.tpl.php on line 18.[/color]Here is my code:[code]<?php function getRandomImages($count = 1){ global $randomImages; srand((float) microtime() * 10000000); $ret = array(); global $ret; $keys = array_rand($randomImages, $count); foreach( $keys as $index ){ array_push( $ret, $randomImages[$index] ); } return $ret; } $randomImages = array(); function addRandomImage( $img = false ) { if( $img ){ global $randomImages; array_push( $randomImages, $img ); }} for( $i=1; $i<41; $i++ ){ addRandomImage( "../../../images/random_".sprintf("%02d", $i).".jpg" ); } ?> <table border="0" cellpadding="0" cellspacing="0" id="header"> <tr> <?php $randomImages = getRandomImages(5); $first = true; foreach( $randomImages as $image ){?> <?php if( $first ){ $first=false; } else{ ?><td width="16"> <img src="../../../images/randompic_divider.gif" width="16" height="52"></td><?php } ?> <td width="52"><img src="<?php echo $image; ?>" width="52" height="52"></td> <?php } ?> [/code]I would really appreciate any help on this in advance. It's really messing with me!Thanks,Emily Link to comment https://forums.phpfreaks.com/topic/30287-array-push-error-but-ive-declared-globals/ Share on other sites More sharing options...
trq Posted December 12, 2006 Share Posted December 12, 2006 Where do you declare $randomImages? Link to comment https://forums.phpfreaks.com/topic/30287-array-push-error-but-ive-declared-globals/#findComment-139376 Share on other sites More sharing options...
emilyfrazier Posted December 12, 2006 Author Share Posted December 12, 2006 Maybe I am not understanding, but I believe I declared $randomImages both in the first and second functions. I declared the array in between the functions. ... I thought making it global would take care of the issue? Thanks for helping,Emily Link to comment https://forums.phpfreaks.com/topic/30287-array-push-error-but-ive-declared-globals/#findComment-139390 Share on other sites More sharing options...
emilyfrazier Posted December 12, 2006 Author Share Posted December 12, 2006 You can see this error here:http://www.retn.org/drupal/graduationsI am really confused on this one, so if you can offer me any futher guidance I would greatly appreciate it!Thanks,Emily Link to comment https://forums.phpfreaks.com/topic/30287-array-push-error-but-ive-declared-globals/#findComment-139677 Share on other sites More sharing options...
utexas_pjm Posted December 12, 2006 Share Posted December 12, 2006 I think I see the problem. Remove this line of code:[code]global $ret;[/code]from the getRandomImages function. Link to comment https://forums.phpfreaks.com/topic/30287-array-push-error-but-ive-declared-globals/#findComment-139684 Share on other sites More sharing options...
emilyfrazier Posted December 12, 2006 Author Share Posted December 12, 2006 Hmm. Just tried that, but it didn't change anything. Am still getting the same errors. Here is my code:[code] <?phpfunction getRandomImages($count = 1){ global $randomImages; srand((float) microtime() * 10000000); $ret = array(); $keys = array_rand($randomImages, $count); foreach( $keys as $index ){ array_push( $ret, $randomImages[$index] ); } return $ret; } $randomImages = array(); function addRandomImage( $img = false ) { if( $img ){ global $randomImages; array_push( $randomImages, $img ); }} for( $i=1; $i<41; $i++ ){ addRandomImage( "../../../images/random_".sprintf("%02d", $i).".jpg" ); } ?><table border="0" cellpadding="0" cellspacing="0" id="header"> <tr> <?php $randomImages = getRandomImages(5); $first = true; foreach( $randomImages as $image ){?> <?php if( $first ){ $first=false; } else{ ?><td width="16"> <img src="../../../images/randompic_divider.gif" width="16" height="52"></td><?php } ?> <td width="52"><img src="<?php echo $image; ?>" width="52" height="52"></td> <?php } ?>[/code]Thanks for helping!Emily Link to comment https://forums.phpfreaks.com/topic/30287-array-push-error-but-ive-declared-globals/#findComment-139690 Share on other sites More sharing options...
emilyfrazier Posted December 12, 2006 Author Share Posted December 12, 2006 Sorry, my mac didn't grab all that I thought it did. The code above is now correctly showing what I have.thanks! Link to comment https://forums.phpfreaks.com/topic/30287-array-push-error-but-ive-declared-globals/#findComment-139844 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.