Timo002
Timo002

Reputation: 3208

PHP array_rand() throws warning I don't expect

I'm debugging my php error_logs to find notices, warnings and errors that I've missed. Now I'm using the php array_rand() function to get 1 element of an array. In normal circumstances this is working without problems, but in my php error logs, there are some warnings regarding the code using array_rand().

PHP Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in ...

I'm using array_rand() in like this:

$arr =
    [
        3 => [
            999 => "10-G2",
            1366 => "11-B2",
            1481 => "14-C3",
        ]
    ];

$randomLoc = array_rand($arr[3], 1);

This will return me 999,1366 or 1481. If this array is empty,

If $arr[3] is emtpy, it will throw an empty warning

Warning: array_rand(): Array is empty in

If $arr[3] not isset, it will throw a undefined offset notice before the warning

Notice: Undefined offset: 3 in

So, how is it possible to get a Second argument has to be between 1 and... warning without the undefined offset notice?

Upvotes: 1

Views: 3643

Answers (3)

Alex Howansky
Alex Howansky

Reputation: 53563

So, how is it possible to get a Second argument has to be between 1 and... warning without the undefined offset notice?

It's not, but that's ok, because you don't want that warning either. Just check that you have a valid array before passing it to array_rand():

if (
    is_array($arr) &&
    array_key_exists(3, $arr) &&
    is_array($arr[3]) &&
    !empty($arr[3])
) {
    $randomLoc = array_rand($arr[3]);
} else {
    $randomLoc = null; // or throw an error?
}

If you're using PHP7+, you can use the new ?? operator to do the same in one shot:

$randomLoc = array_rand($arr[3] ?? [null]);

Upvotes: 0

thisistg
thisistg

Reputation: 190

$randomLoc = isset($arr[3]) && !empty($arr[3]) ? array_rand($arr[3], 1) : null will return null value if $arr is not set or empty.

Plus, you do not have to grep in your log files to find these errors. Your development environment should display notices & warnings. Here's some more information about the error reporting: http://php.net/manual/fr/function.error-reporting.php

Upvotes: 0

Conor Mancone
Conor Mancone

Reputation: 2030

Most likely it is because your array doesn't contain what you think it contains. For instance, this array will give you that error:

$arr =
    [
        3 => []
    ];

$randomLoc = array_rand($arr[3], 1);

Also if I can make a more general suggestion, you shouldn't have to look through your log files to find these things. In your development environment your system should actively tell you if something is broken. In my case, script execution stops and prints a backtrace to the screen with the error message, for any error: fatals, uncaught exceptions, errors, and notices.

Edit:

You said:

If $arr[3] is emtpy, it will throw an empty warning

This is not true. It may depend on version: I'm not super familiar with array_rand. My own testing shows that this:

$arr = [];
$val = array_rand( $arr );

Will end with $val set to null. This:

$arr = [];
$val = array_rand( $arr, 1 );

Will trigger exactly the error you are receiving:

PHP Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in ...

In no case do I get an error about an empty array. I am using PHP 7.0.

Upvotes: 2

Related Questions