use Doctrine\Common\Collections\ArrayCollection; $collection = new ArrayCollection(['apple', 'banana', 'cherry']); $values = $collection->getValues(); // Returns ['apple', 'banana', 'cherry']
use Doctrine\Common\Collections\ArrayCollection; class Product { private $name; private $price; public function __construct($name, $price) { $this->name = $name; $this->price = $price; } public function getName() { return $this->name; } public function getPrice() { return $this->price; } } $product1 = new Product('Apple', 1.0); $product2 = new Product('Banana', 2.0); $collection = new ArrayCollection([$product1, $product2]); $values = $collection->getValues(); // Returns [$product1, $product2] foreach ($values as $product) { echo $product->getName() . ' costs $' . $product->getPrice() . PHP_EOL; }This example demonstrates how to create an ArrayCollection with objects, and then use getValues() to retrieve those objects as an array. It also demonstrates how to access methods on those objects by iterating over the array with a foreach loop. Overall, the ArrayCollection class and its getValues() method are useful for working with collections of objects or simple types in PHP.