我想顯示一個英文逗號分隔的數字。如何添加format_number到我的代碼
此代碼從多個頁面獲取喜歡的數量,並將它們顯示在按喜歡的facebook數量排序的列表中。
function array_sort($array, $on, $order=SORT_ASC)
{
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array;
}
function getLikes($arr){
$urls = "";
// Add urls to check for likes
for($i = 0;$i < count($arr);$i++) {
if($urls != "") $urls .= ",";
$urls .= $arr[$i];
}
// Retreive info from Facebook
$xml = simplexml_load_file("http://api.facebook.com/restserver.php?method=links.getStats&urls=" . $urls);
$likes = array();
// Loop through the result and populate an array with the likes
for ($i = 0;$i < count($arr);$i++) {
$url = $xml->link_stat[$i]->url;
$counts = (int)$xml->link_stat[$i]->like_count;
$likes[] = array('likes' => $counts,'url' => $url);
}
return $likes;
}
$array = array("URL HERE","URL HERE");
$likes = getLikes($array);
$likes = array_sort($likes, 'likes', SORT_DESC);
$english_format_number = number_format($likes, 'likes');
foreach ($likes as $key => $val) {
echo "<li class='facebook'><div class='fb-page'><div class='rank'>" . $key . "</div>" . "<div class='thumb " . $val['url'] . "'><div class='link'>" . $val['url'] . "</div></div>" . "<div class='likes'>" . $val['likes'] . "</div></div></li><br />";
}
該代碼工作正常,順便說一下,我不是一個編碼器,只是剛剛進入它。
我是想的東西添加像
$english_format_number = number_format($likes, 'likes');
但是,當然,我不知道如果我能做到這一點或把它放在哪裏。
任何人都可以幫忙嗎?
我發現這個工作 '$喜歡= array_sort($喜歡, '喜歡',SORT_DESC); foreach($ like as $ key => $ val){ $ final = number_format($ val ['likes'],0,'。',','); echo「
」; }' – user2724009