• 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)
  • Hard to tell without a definition of $quote. Is it possible that $quote has zero elements?

    Thread Starter Jaxia

    (@jaxia)

    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!

    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

    (@jaxia)

    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!

    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

    (@jaxia)

    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?

    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

    (@jaxia)

    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…?

    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

    (@jaxia)

    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!

    Good luck, I’m sure you’ll get it.

    Thread Starter Jaxia

    (@jaxia)

    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!

    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.