注意:未定义的偏移量:返回小于千的数字时为 1

Notice: Undefined offset: 1 when returning number that is below thousand

我从网上获得了这段代码,并在一个从提要 URL 检索数据的函数中实现了它。这是代码:

function subs_count() {
  $x = esc_attr( subs_count_ret() );
  $x_number_format = number_format($x);
  $x_array = explode(',', $x_number_format);
  $x_parts = array('k', 'm', 'b', 't');
  $x_count_parts = count($x_array) - 1;
  $x_display = $x_array[0] . ((int) $x_array[1][0] !== 0 ? '.' . $x_array[1][0] : '');
  $x_display .= $x_parts[$x_count_parts - 1];
  return $x_display;
}

如果数字高于 1k,代码工作正常,但是当检索到的数字为 < 1k 时,它 returns 除了以下错误之外的数字:

Notice: Undefined offset: 1 in C:\wamp\www\preview\wp-content\plugins\xx\xx.php on line 7

Notice: Undefined offset: -1 in C:\wamp\www\preview\wp-content\plugins\xx\xx.php on line 8

有什么想法吗?

谢谢!

我明白了。我将整个代码放在 subs_count_ret() 函数中,它运行良好,另外,我需要这个条件:

if(empty($num) || $num < 1000) return $num;

完整代码:

function subs_count_ret() {
    $xmlData = file_get_contents( 'http://gdata.youtube.com/feeds/api/users/user' ); 
    $xmlData = str_replace('yt:', 'yt', $xmlData); 
    $xml = new SimpleXMLElement($xmlData); 
    $ytio_subs = $xml->ytstatistics['subscriberCount'];
    $num = $ytio_subs;
        if(empty($num) || $num < 1000) return $num;
        $x = round($num);
        $x_number_format = number_format($x);
        $x_array = explode(',', $x_number_format);
        $x_parts = array('k', 'm', 'b', 't');
        $x_count_parts = count($x_array) - 1;
        $x_display = $x;
        $x_display = $x_array[0] . ((int) $x_array[1][0] !== 0 ? '.' . $x_array[1][0] : '');
        $x_display .= $x_parts[$x_count_parts - 1];
        return  $x_display;
}

谢谢大家!祝你有美好的一天!