0

I have a piece of code that uses deprecated each() function. I've attempted to replace it with key() or current() but that just broke my PHP code. What am I missing?

Here's original code snippet:

echo '<div class="cards">';
$i = 0;
reset($cards);
while( (list($card_id, $card) = each($cards)) || $i < 30 ) {
    if( isset($card_id) ) {
        $name = "i$card_id";
        $value = htmlspecialchars($card->label);
    } else {
        $name = $i;
        $value = '';
    }

    echo '
    <div class="card' . (isset($card_id) ? !strlen($card->file_name) ? ' without-image' : '' : ' empty') . '">
        <div class="thumbnail">';
    if( isset($card) && strlen($card->file_name)) {
        echo '<img class="image-zoom" data-href="/assets/' . $testKey . '/' . $card->file_name . '" src="/assets/' . $testKey . '/' . file_name_append($card->file_name, '_s') . '">';
    }
    echo '</div>';
    echo '<input type="text" class="label" name="cardsLabel[' . $name . ']" value="' . $value . '">';
    echo '<input type="file" size="4" class="image" name="cardsImage[' . $name . ']">';
    echo '<span class="remove-dupe uico ui-icon-closethick" onclick="card_form_remove_duplicate(this);"></span>';
    echo "</div>\n";

    ++$i;
}
echo '</div>';
santa
  • 12,234
  • 49
  • 155
  • 255
  • What did you try as a replacement? In most cases you can replace each with key, current and **next**, or just key and next. – jh1711 Oct 16 '18 at 18:22
  • 1
    If you look at https://stackoverflow.com/questions/46492621/how-to-resolve-this-deprecated-function-each-php you may find a `foreach()` will do the job – Nigel Ren Oct 16 '18 at 18:24
  • You could `array_slice()` the array so that there are only 30 in the list (max). Padding it if needed with dummy values for `$card`. – Nigel Ren Oct 16 '18 at 18:47
  • @santa I added this scenario as another example on the duplicate link, I think what I put in the answer there should solve it, but please let me know if it doesn't so I can update it. – Don't Panic Oct 16 '18 at 20:21

0 Answers0