7

変数$output内にある数値を単純に取得して、1000の区切り文字を含む数値に変換しようとしています。現在出力されている数は49995ですが、49,995と表示したいと思います。

いくつかの問題があります。ヘルプ?

function twitter_followers($user = 'mytwitterusername'){
    // Build Twitter api url
    $apiurl = "http://api.twitter.com/1/users/show.json?screen_name={$user}";

    //cache request
    $transient_key = $user . "_twitter_followers";

    // If cached (transient) data are used, output an HTML
    // comment indicating such
    $cached = get_transient( $transient_key );

    if ( false !== $cached ) {
        return $cached;
    }

    // Request the API data, using the constructed URL
    $remote = wp_remote_get( esc_url( $apiurl ) );

    // If the API data request results in an error, return
    // an appropriate comment
    if ( is_wp_error( $remote ) ) {
        return '<p>Twitter unaviable</p>';
    }

    // If the API returns a server error in response, output
    // an error message indicating the server response.
    if ( '200' != $remote['response']['code'] ) {
        return '<p>Twitter responded with an HTTP status code of '. esc_html( $remote['response']['code']) . '</p>';
    }

    // If the API returns a valid response, the data will be
    // json-encoded; so decode it.
    $data = json_decode( $remote['body'] );

    $output = $data->followers_count;
    $followers = number_format($output,2,'.',',');

    set_transient( $transient_key, $output, 600 );

    return $followers;
}
4

2 に答える 2

12

私は次のコードをテストしました、そしてそれは働きます:

$output = 49995;
$followers = number_format( $output , 0 , '.' , ',' );
echo $followers;

コードが機能しない理由がわかりません。また、小数点が必要な場合を除いて、必ず2番目のパラメーターを0に設定してください。おそらく、$ outputの値は最初は文字列であり、number_format()を通過させる前に、整数としてキャストする必要がありますか?

于 2012-07-12T20:28:48.150 に答える
1

あなたのnumber_formatは正しいようです。試してみてください

$output = intval($data->followers_count);

呼び出す前に、値をデコードした後に問題が発生している可能性があります。

于 2012-07-12T20:27:01.713 に答える