php - sorting an array of objects by datetime variable -
i want sort array of object datetime variable, dates situated in future , want date thats closest current date first one.
i'm using following controlleraction in symfony2:
public function fixturesaction(){ if (false === $this->get('security.context')->isgranted('is_authenticated_fully')) { throw $this->createaccessdeniedexception('unable access page!'); } $user = $this->get('security.context')->gettoken()->getuser(); $team = $this->getdoctrine() ->getrepository('loginloginbundle:team') ->findbyuseruserid($user->getuserid()); $matchgameshome = $this->getdoctrine() ->getrepository('loginloginbundle:matchgame') ->findbyhometeam($team[0]->getname()); $matchgamesaway = $this->getdoctrine() ->getrepository('loginloginbundle:matchgame') ->findbyawayteam($team[0]->getname()); $matchgames = array_merge($matchgameshome, $matchgamesaway); $sorted = usort($matchgames, function($a, $b) { return $a->date->format('u') - $b->date->format('u'); }); return $this->render('loginloginbundle:default:fixtures.html.twig', array("matcharray"=>$sorted)); }
where sorting:
$sorted = usort($matchgames, function($a, $b) { return $a->date->format('u') - $b->date->format('u'); });
this yields me following error:
error: cannot access private property login\loginbundle\entity\matchgame::$date in c:\wamp\www\socpronetbeans\src\login\loginbundle\controller\defaultcontroller.php line 675
line 675 following:
return $a->date->format('u') - $b->date->format('u');
what proper way sort array?
since don't know line error speaks of, small shot in dark:
replace
return $a['date']->format('u') - $b['date']->format('u');
with
return $a->date->format('u') - $b->date->format('u');
Comments
Post a Comment