Array_multisort不按名称排序

时间:2015-05-16 23:15:20

标签: php array-multisort

另一个Array_multisort帖子对我没有帮助。我按名称排序了2个数组。当我使用array_multisort时,它会按ID进行排序。我怎样才能解决这个问题?我正在使用Joomla 2.5。提前谢谢。

$query = "SELECT DISTINCT(t.id) as id,
                 t.t_name as name, t.t_city as city,
                 (count(pd.rs)) as pdc
    FROM #__bl_teams as t
    JOIN #__bl_paid as pd ON pd.t_id = t.id
        AND pd.rs=1 AND ((MID(pd.r_date,1,10)) >= t.start_date)
    WHERE (t.id IN($teams)) GROUP BY t.id ORDER BY t.t_name ASC";

$db - > setQuery($query);
$players1 = $db - > loadObjectList();

///----same query as above, but return array of team Id's---///

    $query = "SELECT DISTINCT(t.id)
    FROM #__bl_teams as t 
    JOIN #__bl_paid as pd ON pd.t_id = t.id AND pd.rs=1 AND ((MID(pd.r_date,1,10)) >= t.start_date)
    WHERE (t.id IN($teams)) 
    GROUP BY t.id ORDER BY t.t_name ASC";
    $db->setQuery($query);
    $players1a = $db->loadResultArray();

    $players3 = implode(',',$players1a); 

///---------------------------------------------------------///

    $query = "SELECT DISTINCT(t.id) as id, t.t_name as name, t.t_city as city
    FROM #__bl_teams as t 
    WHERE (t.id IN($teams)) AND (t.id NOT IN($players3))
    GROUP BY t.id ORDER BY t.t_name ASC";
    $db->setQuery($query);
    $players2 = $db->loadObjectList();

    $players = array_merge($players1,$players2);


if ($players) {
    $play1 = array();
    foreach($players as $play) {
        $play1[] = $play - > name;
    }
}
array_multisort($play1, SORT_ASC, $players);

var_dump($players);
array(32) {
    [0] = > object(stdClass) #441 (4) { ["id"]= > string(3)
    "455" ["name"] = > string(6)
    "Barrow" ["city"] = > string(6)
    "Barrow" ["pdc"] = > string(1)
    "1"
}
[1] = > object(stdClass) #440 (4) { ["id"]= > string(3)
"480" ["name"] = > string(5)
"South" ["city"] = > string(9)
"Anchorage" ["pdc"] = > string(1)
"2"
}
[2] = > object(stdClass) #439 (3) { ["id"]= > string(5)
"16977" ["name"] = > string(5)
"atom1" ["city"] = > string(3)
"aaa"
}
[3] = > object(stdClass) #438 (3) { ["id"]= > string(3)
"456" ["name"] = > string(8)
"Bartlett" ["city"] = > string(9)
"Anchorage"
}
[4] = > object(stdClass) #437 (3) { ["id"]= > string(3)
"457" ["name"] = > string(7)
"Chugiak" ["city"] = > string(11)
"Eagle River"
}
[5] = > object(stdClass) #436 (3) { ["id"]= > string(3)
"458" ["name"] = > string(6)
"Colony" ["city"] = > string(6)
"Palmer"
}
[6] = > object(stdClass) #435 (3) { ["id"]= > string(3)
"459" ["name"] = > string(14)
"Delta Junction" ["city"] = > string(14)
"Delta Junction"
}
[7] = > object(stdClass) #434 (3) { ["id"]= > string(3)
"460" ["name"] = > string(6)
"Dimond" ["city"] = > string(9)
"Anchorage"
}
[8] = > object(stdClass) #433 (3) { ["id"]= > string(3)
"461" ["name"] = > string(11)
"Eagle River" ["city"] = > string(11)
"Eagle River"
}
[9] = > object(stdClass) #432 (3) { ["id"]= > string(3)
"462" ["name"] = > string(4)
"East" ["city"] = > string(9)
"Anchorage"
}
[10] = > object(stdClass) #431 (3) { ["id"]= > string(3)
"463" ["name"] = > string(7)
"Eielson" ["city"] = > string(11)
"Eielson AFB"
}
[11] = > object(stdClass) #430 (3) { ["id"]= > string(3)
"464" ["name"] = > string(5)
"Homer" ["city"] = > string(5)
"Homer"
}
[12] = > object(stdClass) #429 (3) { ["id"]= > string(3)
"465" ["name"] = > string(7)
"Houston" ["city"] = > string(8)
"Big Lake"
}
[13] = > object(stdClass) #428 (3) { ["id"]= > string(3)
"466" ["name"] = > string(14)
"Juneau-Douglas" ["city"] = > string(6)
"Juneau"
}
[14] = > object(stdClass) #427 (3) { ["id"]= > string(3)
"467" ["name"] = > string(13)
"Kenai Central" ["city"] = > string(5)
"Kenai"
}
[15] = > object(stdClass) #426 (3) { ["id"]= > string(3)
"468" ["name"] = > string(9)
"Ketchikan" ["city"] = > string(9)
"Ketchikan"
}
[16] = > object(stdClass) #425 (3) { ["id"]= > string(3)
"469" ["name"] = > string(6)
"Kodiak" ["city"] = > string(6)
"Kodiak"
}
[17] = > object(stdClass) #424 (3) { ["id"]= > string(3)
"470" ["name"] = > string(7)
"Lathrop" ["city"] = > string(9)
"Fairbanks"
}
[18] = > object(stdClass) #423 (3) { ["id"]= > string(3)
"471" ["name"] = > string(15)
"Monroe Catholic" ["city"] = > string(9)
"Fairbanks"
}
[19] = > object(stdClass) #422 (3) { ["id"]= > string(3)
"472" ["name"] = > string(7)
"Nikiski" ["city"] = > string(7)
"Nikiski"
}
[20] = > object(stdClass) #421 (3) { ["id"]= > string(3)
"473" ["name"] = > string(10)
"North Pole" ["city"] = > string(10)
"North Pole"
}
[21] = > object(stdClass) #420 (3) { ["id"]= > string(3)
"474" ["name"] = > string(6)
"Palmer" ["city"] = > string(6)
"Palmer"
}
[22] = > object(stdClass) #419 (3) { ["id"]= > string(3)
"475" ["name"] = > string(7)
"Service" ["city"] = > string(9)
"Anchorage"
}
[23] = > object(stdClass) #418 (3) { ["id"]= > string(3)
"476" ["name"] = > string(6)
"Seward" ["city"] = > string(6)
"Seward"
}
[24] = > object(stdClass) #417 (3) { ["id"]= > string(3)
"477" ["name"] = > string(5)
"Sitka" ["city"] = > string(5)
"Sitka"
}
[25] = > object(stdClass) #416 (3) { ["id"]= > string(3)
"478" ["name"] = > string(7)
"Skyview" ["city"] = > string(8)
"Soldotna"
}
[26] = > object(stdClass) #415 (3) { ["id"]= > string(3)
"479" ["name"] = > string(8)
"Soldotna" ["city"] = > string(8)
"Soldotna"
}
[27] = > object(stdClass) #414 (3) { ["id"]= > string(3)
"481" ["name"] = > string(16)
"Thunder Mountain" ["city"] = > string(6)
"Juneau"
}
[28] = > object(stdClass) #413 (3) { ["id"]= > string(3)
"482" ["name"] = > string(6)
"Valdez" ["city"] = > string(6)
"Valdez"
}
[29] = > object(stdClass) #412 (3) { ["id"]= > string(3)
"483" ["name"] = > string(7)
"Wasilla" ["city"] = > string(7)
"Wasilla"
}
[30] = > object(stdClass) #411 (3) { ["id"]= > string(3)
"484" ["name"] = > string(4)
"West" ["city"] = > string(9)
"Anchorage"
}
[31] = > object(stdClass) #410 (3) { ["id"]= > string(3)
"485" ["name"] = > string(11)
"West Valley" ["city"] = > string(9)
"Fairbanks"
}
}

1 个答案:

答案 0 :(得分:1)

我认为array_multisort()不会像您认为的那样有效。

您希望按name对查询返回的值进行排序,对吗? 在这种情况下,您要寻找的是usort()

...

$players = $db - > loadObjectList();

function nameComparator($a, $b) {
    return strcmp(strtoupper($a->name), strtoupper($b->name));
}

usort($players, "nameComparator");