I change this while for one for but I don't know if is correct

public function DisplayMenu($buttons)
  {
    echo "<!-- menu -->
    <nav>";


    while (list($name, $url) = each($buttons)) {
      $this->DisplayButton($name, $url, 
               !$this->IsURLCurrentPage($url));
    }
    
echo "</nav>\n"; } ```
//the new code foreach ($buttons as list($name,$url)){ $this->DisplayButton($name, $url, !$this->IsURLCurrentPage($url)); }

    each() was deprecated with the release of PHP 7.2.

    Alternative strategies include foreach(), a for() loop with next, write your own each(), etc. The manual page has some helpful comments and some "roll your own" strategies.

    As well as the page @dalecosp links, which says

    Return Values

    Returns the current key and value pair from the array array. This pair is returned in a four-element array, with the keys 0, 1, key, and value. Elements 0 and key contain the key name of the array element, and 1 and value contain the data.

    you'll also want to look at the page for the loop's replacement, foreach, which says

    foreach (iterable_expression as $key => $value)
        statement

    ... will additionally assign the current element's key to the $key variable on each iteration.

    What I'm getting at is, it's worth reading the manual.

    Edit: I went digging through old versions of the manual, back to PHP 4, and found where the foreach page still said this:

    The following are also functionally identical:

    <?php
    $arr = array("one", "two", "three");
    reset($arr);
    while (list($key, $value) = each($arr)) {
       echo "Key: $key; Value: $value<br />\n";
    }
    
    foreach ($arr as $key => $value) {
       echo "Key: $key; Value: $value<br />\n";
    }
    ?>

    Why any PHP book written in the last twenty years would still use while(...each...) is probably down to lazy editing of even earlier versions.

    Write a Reply...