iterator_to_array() to create a specific array data structure

Code
<?php

class FooIterator extends ArrayIterator
{
    public function 
key()
    {
        return 
$this->current()->getKey();
    }
}

class 
Foo
{
    public function 
__construct($key$value)
    {
        
$this->key $key;
        
$this->value $value;
    }
    
    public function 
getKey()
    {
        return 
$this->key;
    }
    
    public function 
getName()
    {
        return 
$this->name;
    }
}

$arr = [
    new 
Foo('aaa','value a'),
    new 
Foo('bbb','value b'),
    new 
Foo('ccc','value c'),
];

$iterator = new FooIterator($arr);

$arr iterator_to_array($iterator);

dump(array_key_exists('bbb'$arr));
Result
^ true
Used Versions
PHP 8.2, Laminas MVC 3.2, Symfony 5.2, Laravel 8.28, PHPUnit 9.5, Doctrine ORM 2.8