PHP5 to PHP7 error array_rand() and foreach()

My website was working nicely, then my host decided it was time to go with PHP 7.

I have this error:

Warning : array_rand(): Second argument has to be between 1 and the number of elements in the array in /home/HIDDEN/templates/main-template.php on line 546

Warning : Invalid argument supplied for foreach() in /home/HIDDEN/templates/main-template.php on line 547

And my code starting with line 546 and showing line 547:

$randlist = array_rand($useabletowns, count($useabletowns)-1);
foreach ($randlist as $value)

I bet that $useabletowns is empty…
If you want to shuffle your array you could simply use

array_shuffle(useabletowns);

I have been trying to get this debugged for a few hours (almost 5).

This is what my array ends up;

Array
(
[0] => 0
[1] => 1
[2] => 3
)

It’s supposed to be 4 values. I’m working from a list of about 120.

         $maxtownlistsize = 4;

// $useabletowns = array_values($useabletowns);

          if ($useabletownscount > $maxtownlistsize) {
           $new_array = array();
           $randlist = array_rand($useabletowns, $maxtownlistsize);

// $randlist = array_values($randlist);

if (!empty($randlist)) {
           foreach ($randlist as $value) 
            { 
             array_push($new_array, $useabletowns[$value]);
            }

			}
           $useabletownsrand = $new_array;
           }
          elseif ($useabletownscount == 1) {
           $useabletownsrand = $useabletowns;
           }
          elseif ($useabletownscount <= $maxtownlistsize) {
           $new_array = array();
           $randlist = array_rand($useabletowns, count($useabletowns)-1);
           foreach ($randlist as $value)
            {
             array_push($new_array, $useabletowns[$value]);
            }
           $useabletownsrand = $new_array;
          }

I obviously don’t know what I’m doing.

Rather than ask about your attempted solution to the Real problem, tell us what you are trying to solve with this code.

I have several different pages. The pages take text from a list pertaining to the town or zip code. Town names. So some pages will have one name associated with it, and some will have up to 120 names associated with it.

I want to take a maximum of 4 of the names and output that into a sentence, for instance, “from W to X, and from Y to Z.”

However, if there’s only one town name associated with that page, it will just say “we service X”.

I had this script working beautifully for years and all of a sudden after PHP7 I can’t get this to work without errors.

$maxtownlistsize = 4
$useabletownscount = 117 in this instance
$randlist = 3 (I don’t know how it went to 3)
$useabletownsrand should be Z, W, X, Y (or something randomized) if 4 or more towns, 1 name if just the one town.

Some code used further in the page is this:

elseif (($useabletownscount == 4) || ($useabletownscount > 4)) {
print ". From $useabletownsrand[0] to $useabletownsrand[1], and from $useabletownsrand[2] to $useabletownsrand[3], TEXT HERE ";

I changed this line

to

and I get better results, but still that error.

**Warning** : array_rand(): Second argument has to be between 1 and the number of elements in the array in  **/home/HIDDEN/templates/main-template.php**  on line  **563**

**Warning** : Invalid argument supplied for foreach() in  **/home/HIDDEN/templates/main-template.php**  on line  **564**

I don’t understand, because I would think count($useabletowns) is the number of elements in the array. I can see how count($useabletowns)-1 was throwing it off, but don’t understand this one.

You should really post a full example including data, use var_export() for that. But i do not see how array_rand($array, count($array)) should give any benefit, as you only would get a full list of keys - this list is not even shuffled.

<?php
$array = [1,2,3,4,5,6,7,8,9];
var_dump(array_keys($array) === array_rand($array, count($array))); // true

I wrapped my head around it. I’m adding a bunch more elseif conditions to this and it seems to be working. Just have to refine it.

Sponsor our Newsletter | Privacy Policy | Terms of Service