doushou3814 2013-08-28 05:45 采纳率: 100%
浏览 46

如何将format_number添加到我的代码中

I am trying to display a number that has the english comma separation.

This code gets the number of likes from multiple pages and displays them in a list ordered by number of facebook likes.

    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 />";
}

The code works fine, by the way I am not a coder, and am only just getting into it.

I was trying to add in something like

    $english_format_number = number_format($likes, 'likes');

But of course I have no idea if I can do that or where to put it.

Can anyone help?

  • 写回答

1条回答 默认 最新

  • doushi9444 2013-08-28 06:01
    关注

    you use wrong parameter in number_format function. the correct format is number_format(number,decimals,decimalpoint,separator). so change you code to

    $english_format_number = number_format($likes, 0, ',', '.') . 'likes';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题