0
我有4個數組,我試圖從最高到最低排序屬性視圖。我想弄清楚如何排序合併數組。Symfony2 - 如何對合並的對象數組進行排序?
現在與合併陣列我有從1集最高到最低的意見,然後從第2集最高到最低的意見。
如何對兩組進行排序,以便在一個合併數組中有4個數組的最高到最低視圖?
(例如,目前:合併數組1:最高最低視圖/合併陣列2:最高到最低的觀點---我想最高到最低的所有4個1組)
我有2套
private static function postSort($post, $post2)
{
return $post->getViews() == $post2->getViews() ? 0 : ($post->getViews() < $post2->getViews()) ? 1: -1;
}
private static function postSort2($post3, $post4)
{
return $post3->getViews() == $post4->getViews() ? 0 : ($post3->getViews() < $post4->getViews()) ? 1: -1;
}
我使用usort從最高到最低的意見進行排序:只用1 postSort和一個我們解決
$posts = $this->getDoctrine()->getRepository('AcmeDemoBundle:Post')
->getPosts();
$posts2 = $this->getDoctrine()->getRepository('AcmeDemoBundle:Post2')
->getPosts2();
$posts3 = $this->getDoctrine()->getRepository('AcmeDemoBundle:Post3')
->getPosts3();
$posts4 = $this->getDoctrine()->getRepository('AcmeDemoBundle:Post4')
->getPosts4();
$postTotal1 = array_merge($posts, $posts2);
usort($postTotal1, array($this, 'postSort'));
$postTotal2 = array_merge($posts3, $posts4);
usort($postTotal2, array($this, 'postSort2'));
$total = array_merge($postTotal, $postTotal2);