Title: array_rand help
Last modified: August 18, 2016

---

# array_rand help

 *  [Jaxia](https://wordpress.org/support/users/jaxia/)
 * (@jaxia)
 * [21 years ago](https://wordpress.org/support/topic/array_rand-help/)
 * This isn’t a WP question yet, but it will be 🙂
 * I’m trying to write a plugin, and I am almost there. But, I keep getting these
   weird errors and was wondering if someone could help:
 * array_rand(): Second argument has to be between 1 and the number of elements 
   in the array
 * This is the array bit of code:
 * $rand_key=array_rand($quote, 1);
    list($words, $author, $image, $link) = explode(“
   |”, trim($quote[$rand_key]));
 * Hopefully, I’m just missing something obvious.
    Thank you so much!

Viewing 13 replies - 1 through 13 (of 13 total)

 *  [ColdForged](https://wordpress.org/support/users/coldforged/)
 * (@coldforged)
 * [21 years ago](https://wordpress.org/support/topic/array_rand-help/#post-201872)
 * Hard to tell without a definition of `$quote`. Is it possible that `$quote` has
   zero elements?
 *  Thread Starter [Jaxia](https://wordpress.org/support/users/jaxia/)
 * (@jaxia)
 * [21 years ago](https://wordpress.org/support/topic/array_rand-help/#post-201885)
 * Sorry. I’m so new I don’t even know what’s important!
 * $quotefile=”quotes.txt”;
    $dir=dirname($_SERVER[‘SCRIPT_FILENAME’]); $url=dirname(
   $_SERVER[‘PHP_SELF’]);
 * if (file_exists($dir.”/”.$quotefile))
    $quote=file($dir.”/”.$quotefile);
 * And yes, the file has data in it.
 * Thanks!
 *  [ColdForged](https://wordpress.org/support/users/coldforged/)
 * (@coldforged)
 * [21 years ago](https://wordpress.org/support/topic/array_rand-help/#post-201888)
 * Try doing a `var_dump()` of `$quote` right before your `array_rand()` call. Then,
   try removing the `1` parameter from the call just to see if that works.
 *  Thread Starter [Jaxia](https://wordpress.org/support/users/jaxia/)
 * (@jaxia)
 * [21 years ago](https://wordpress.org/support/topic/array_rand-help/#post-201894)
 * Hmm
 * When I do that, it only prints this:
    array(0) { }
 * That’s it.
 * This is what the whole thing looks like right now:
 * $quotetitle=”Words of Wisdom”;
    $quotefile=”quotes.txt”;
 * $dir=dirname($_SERVER[‘SCRIPT_FILENAME’]);
    $url=dirname($_SERVER[‘PHP_SELF’]);
 * function STBquote() {
    if (file_exists($dir.”/”.$quotefile)) $quote=file($dir.”/”.
   $quotefile); var_dump($quote); $rand_key=array_rand($quote); list($text, $author,
   $link, $linktext) = explode(“|”, trim($quote[$rand_key]));
 *  echo “<div>”;
    echo “<h2>”.$quotetitle.”</h2>”; if (isset($text) && $text!=””){
   echo “<p class=\”pullquote\”>”.$text.””; } if (isset($author) && $author!=””){
   echo “”.$author.””; } if (isset($link) && $link!=”” && isset($linktext) && $linktext!
   =””) { echo ““.$linktext.”“; } echo “</div>”; }
 * ?>
 * Ideas? I really appreciate your help!
 *  [ColdForged](https://wordpress.org/support/users/coldforged/)
 * (@coldforged)
 * [21 years ago](https://wordpress.org/support/topic/array_rand-help/#post-201898)
 * You’ve defined `$quotetitle`, `$quotefile`, `$dir`, and `$url` in the global 
   namespace, but you haven’t provided references from within your function to the
   global variables. Either move those existing declarations within the function
   or create global references (e.g. `global $url, $dir, ...`) at the top of your
   function.
 *  Thread Starter [Jaxia](https://wordpress.org/support/users/jaxia/)
 * (@jaxia)
 * [21 years ago](https://wordpress.org/support/topic/array_rand-help/#post-201901)
 * Ah. Okay. I moved them within the function. Now, it’s getting closer because 
   it outputs the title, but I still get this error:
 * First argument has to be an array
 * I tried removing the var dump and adding the 1 back in and it didn’t help. Does
   this mean there is a problem with my txt file?
 *  [ColdForged](https://wordpress.org/support/users/coldforged/)
 * (@coldforged)
 * [21 years ago](https://wordpress.org/support/topic/array_rand-help/#post-201906)
 * What did you get for the `var_dump` when it was in? And first argument to _what_
   has to be an array?
 *  Thread Starter [Jaxia](https://wordpress.org/support/users/jaxia/)
 * (@jaxia)
 * [21 years ago](https://wordpress.org/support/topic/array_rand-help/#post-201913)
 * Oh! It says NULL. (Honestly, I didn’t understand what the var_dump was doing 
   before)
 * array_rand(): First argument has to be an array
 * But I suppose the NULL explains that. I guess I don’t have my file defined right…?
 *  [ColdForged](https://wordpress.org/support/users/coldforged/)
 * (@coldforged)
 * [21 years ago](https://wordpress.org/support/topic/array_rand-help/#post-201914)
 * Bear in mind, if that `if (file_exists($dir."/".$quotefile))` condition fails,`
   $quote` will be NULL as well. Right now that’s just, to be honest, poorly structured.
   You should at the minimum have that condition bound the entire logic block or
   simply return outright. But, this isn’t PHP 101 either :).
 *  Thread Starter [Jaxia](https://wordpress.org/support/users/jaxia/)
 * (@jaxia)
 * [21 years ago](https://wordpress.org/support/topic/array_rand-help/#post-201918)
 * hehe – Thank you.
    I will look at it some more tonight and see if I can figure
   it out. Right now, I have to go take a few finals.
 * Thanks again!
 *  [ColdForged](https://wordpress.org/support/users/coldforged/)
 * (@coldforged)
 * [21 years ago](https://wordpress.org/support/topic/array_rand-help/#post-201920)
 * Good luck, I’m sure you’ll get it.
 *  Thread Starter [Jaxia](https://wordpress.org/support/users/jaxia/)
 * (@jaxia)
 * [21 years ago](https://wordpress.org/support/topic/array_rand-help/#post-202329)
 * ColdForged, if you’re around — I got it! I did TWO explodes. I needed to explode
   by line, and then on the pipes.
 * Thanks for telling me about var_dump — it helped a lot!
 *  [ColdForged](https://wordpress.org/support/users/coldforged/)
 * (@coldforged)
 * [21 years ago](https://wordpress.org/support/topic/array_rand-help/#post-202333)
 * Great news, glad you got it sorted out.

Viewing 13 replies - 1 through 13 (of 13 total)

The topic ‘array_rand help’ is closed to new replies.

 * 13 replies
 * 2 participants
 * Last reply from: [ColdForged](https://wordpress.org/support/users/coldforged/)
 * Last activity: [21 years ago](https://wordpress.org/support/topic/array_rand-help/#post-202333)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
