1

I'm changing server, I switch from centos to ubuntu 20.04 with php 8.

Many of my sites on the server use this old class: https://github.com/xmyl/Snoopy-2.0/blob/master/Snoopy.class.php

The sites have been there for a long time and I use this class to access the various username and password protected areas of my affiliate programs, read the xml/rss/csv files, and process them automatically.

The problem is that this class often uses the each() function which was previously deprecated and now eliminated in php 8.

I need to change the server now (it wasn't expected), and get this class to work somehow. Afterwards I can calmly review all the scripts, update them, modify them etc, but now I need a quick and easy solution to make it work.

I tried changing each() to current(), and then to add this:

/**
 * Adds the depreciated each() function back into 7.2
 */
if (!function_exists('each')) {
    function each($arr) {
        $key = key($arr);
        $result = ($key === null) ? false : [$key, current($arr), 'key' => $key, 'value' => current($arr)];
        next($arr);
        return $result;
    }
}

It didn't work much, the script ends in: Fatal error: Allowed memory size....

There is a quick and easy way to get it to work? (temporarily) Or a quick alternative that works as well as snoopy?


Just to close... solved in 5 seconds... the solution...

if (!function_exists('each')) {
    function each(array &$array) {
        $value = current($array);
        $key = key($array);

        if (is_null($key)) {
            return false;
        }

        // Move pointer.
        next($array);

        return array(1 => $value, 'value' => $value, 0 => $key, 'key' => $key);
    }
}

https://gist.github.com/k-gun/30dd2bf8b22329a2dbc11a045aed3859

Dharman
  • 30,962
  • 25
  • 85
  • 135
alebal
  • 5,838
  • 3
  • 10
  • 28

1 Answers1

1

You need to pass the array to your each() function by reference. The way it is currently written the array is copied into the function, and that copy is having it's internal pointer incremented without affecting the original.

function each(&$arr) {
 /*...*/
}
Samuel
  • 1,073
  • 11
  • 22