Magento: Creating Varien Object & Collection from any Array data

Varien_Object and Varien_Collection are the parent/super class for most of the Magento Models and Collections respectively.

The path for Varien_Object is lib/Varien/Object & for Varien_Collection is lib/Varien/Data/Collection.php.

This article shows how you can create new Varien objects and collections from any array or object data you have.

Here is the code:-


/**
 * Creating new varien collection 
 * for given array or object
 *
 * @param array|object $items	Any array or object
 * @return Varien_Data_Collection $collection
 */
public function getVarienDataCollection($items) {
	$collection = new Varien_Data_Collection();				
	foreach ($items as $item) {
		$varienObject = new Varien_Object();
		$varienObject->setData($item);
		$collection->addItem($varienObject);
	}
	return $collection;
}

More details on Varien Data Objects & Collections can be found on this Magento Knowledge Base Article:
Magento for Developers: Part 8 – Varien Data Collections