PHP PHP Error (array_rand argument)

Discussion in 'Programming Languages' started by HTML, Mar 18, 2014.

  1. HTML

    HTML Big Board Coder

    259
    125
    +36
    Error on my site.
    Custom script.

    Error Code:
    Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in /home/audio/public_html/audiomarket/apps/index/controllers/index.php on line 108

    Lines 107-109 of /apps/index/controllers/index.php -
    Code:
    Line 107: #RANDOM CATEGORIES
    Line 108: $randCategories = array_rand($mainCategories, 5);
    Line 109: abr('randCategories', $randCategories);
     
  2. LotusJeff

    LotusJeff Enthusiast

    150
    43
    +34
    Usually, this error means your input variable is not a properly formatted array or the array is empty.

    Do a print_r of $mainCategories to validate it is a properly formatted one dimensional array.

    Then validate the array has at least 5 elements. If you have less than 5 elements in your input variable, you will get this error. It is very common to use an if statement to determine your second variable is larger than the number of elements within the error.
    Code:
    $rnd_amount_needed = 5;
    if (count($array) < $rnd_amount_needed) {$rnd_amount_needed = count($array);}
    $rnd_results = array_rand($array, $rnd_amount_needed);

    LotusJeff
     
  3. HTML

    HTML Big Board Coder

    259
    125
    +36
    Thanks, Jeff.
    Here is the printed array of $mainCategories.
    I am still unsure of what is causing the issue.
    I have not the required number of elements, I guess?
    I don't know how to fix it :/?

    Thanks

    Code:
    Array
    (
        [205] => Array
            (
                [id] => 205
                [sub_of] => 0
                [meta_title] => Single Release
                [meta_keywords] => Single Release
                [meta_description] => Single Release
                [name] => Single Release
                [text] => 
                [visible] => true
                [order_index] => 1
                [clear_text] => 
            )
    
        [206] => Array
            (
                [id] => 206
                [sub_of] => 0
                [meta_title] => Album Release
                [meta_keywords] => Album Release
                [meta_description] => Album Release
                [name] => Album Release
                [text] => 
                [visible] => true
                [order_index] => 2
                [clear_text] => 
            )
    
    )
    
     
    Last edited: Mar 19, 2014
  4. HTML

    HTML Big Board Coder

    259
    125
    +36
    I have fixed the occurring array_rand argument issue.
    You were correct, it was the input causing the issue.

    But now I have an undefined index error.
    Undefined index: qnews in /home/audio/public_html/audiomarket/apps/qnews/admin/list.php on line 3

    Code:
    Line 1: <?php
    Line 2: _setView ( __FILE__ );
    Line 3: _setTitle ( $langArray ['qnews'] );
    Does this mean qnews is not defined and that I would have to define it in the language file?
     
  5. LotusJeff

    LotusJeff Enthusiast

    150
    43
    +34
    That would be my guess.

    I am not familiar with VB's inner workings. Any VB experienced people have any guidance?

    jeff
     
  6. HTML

    HTML Big Board Coder

    259
    125
    +36
    I assume you are speaking of the vBulletin script rather than Visual Basic, the third-generation event-driven programming language, as that is mostly for application programming.

    (If you're wondering why I was confused, I am not greatly knowledgable of Visual Basic, but have heard of it before. The forum script "vBulletin" is most commonly abbreviated and stylized as "vB", whereas Visual Basic is commonly abbreviated and stylized in all caps, "VB", which is the reason for my slight confusion.)

    Anyway, as I said in my first post, I am using a custom audio marketplace script similar to Beatport.com. This was my latest venture, an audio marketplace, combined with an audio enthusiast and production forum community, similar to KVRAudio or DubstepForum.
     
    Last edited: Mar 19, 2014
  7. TrixieTang

    TrixieTang Politically Incorrect

    8,509
    1,522
    +2,806
    I've removed the last few posts. Let's just let the record say that it's a custom script and end it at that, no need to argue over it.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.