Asked  7 Months ago    Answers:  5   Viewed   26 times

Is there an SPL Reverse array iterator in PHP? And if not, what would be the best way to achieve it?

I could simply do

$array = array_reverse($array);
foreach($array as $currentElement) {}

or

for($i = count($array) - 1; $i >= 0; $i--)
{

}

But is there a more elegant way?

 Answers

61

There is no ReverseArrayIterator to do that. You can do

$reverted = new ArrayIterator(array_reverse($data));

or make that into your own custom iterator, e.g.

class ReverseArrayIterator extends ArrayIterator 
{
    public function __construct(array $array)
    {
        parent::__construct(array_reverse($array));
    }
}

A slightly longer implementation that doesn't use array_reverse but iterates the array via the standard array functions would be

class ReverseArrayIterator implements Iterator
{
    private $array;

    public function __construct(array $array)
    {
        $this->array = $array;
    }

    public function current()
    {
        return current($this->array);
    }

    public function next()
    {
        return prev($this->array);
    }

    public function key()
    {
        return key($this->array);
    }

    public function valid()
    {
        return key($this->array) !== null;
    }

    public function rewind()
    {
        end($this->array);
    }
}
Wednesday, March 31, 2021
 
millenomi
answered 7 Months ago
22

So the problem was that the $class being returned to spl_autoload_register was the namespaceclass name, with the backslash intact. So when I instantiated a new object:

$myObj = new FooClass1();

The include path became /var/www/myApp/classes/FooClass1.php, the backslash breaking the path.

I implemented this to fix the backslash, and it now works, although I do not know why this is necessary.

spl_autoload_register(function($class) {
    include 'classes/' . str_replace('\', '/', $class) . '.class.php';
});
Wednesday, March 31, 2021
 
superfell
answered 7 Months ago
14

Your error is in your get_class() method. $class_name contains the fully qualified classname with it's namespace. In your case you have to strip the namespace from your classname.

$file_name = 'class_'.strtolower(array_pop(explode('\', $class_name))).'.php';

I highly recommend using PSR-0 standard for autoloading. If you will be using any libraries it is very likely that they are using the same standard and you only have one autoloader.

Saturday, May 29, 2021
 
aaronhuisinga
answered 5 Months ago
68

The code to be substituted in place of // some code here in the question is:

for (int i = 0; i < arr.Length / 2; i++)
{
   int tmp = arr[i];
   arr[i] = arr[arr.Length - i - 1];
   arr[arr.Length - i - 1] = tmp;
}

You should iterate only through the first half of the array (arr.Length / 2). If you iterate through the whole array (arr.Length), it will be reversed twice, yielding the same element order as before it started.

Thursday, July 15, 2021
 
francadaval
answered 3 Months ago
21

you could have a second loop of

$batch_of = 5;
$batch = array_chunk($my_array, $batch_of);
foreach($batch as $b) {

    foreach ($b as $key => $value) {

        echo $value;
    }
   sleep (5);
}

Which would work as intended

Wednesday, August 4, 2021
 
palAlaa
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :