What is the easiest way to map array of objects to key-value array of objects, where key is some property of an object?
For example:
class Cartoon
{
public $title;
public function __construct($title)
{
$this->title = $title;
}
}
$cartoons = array(new Cartoon('Tom and Jerry'), new Cartoon('Cheburashka'));
$mappedCartoons = array();
foreach ($cartoons as $cartoon)
{
$mappedCartoons[$cartoon->title] = $cartoon;
}
print_r ($mappedCartoons);
PS. I wonder if iteration and the extra variable $mappedCartoons can be disposed of?