当数组中的值为0时,它变为 -

时间:2013-04-04 12:33:46

标签: php arrays

我有一个这样的数组:

Array
(
    [played] => 2
    [random] => 0
    [win] => 2
    [draws] => 11
    [passes] => 2
    [swaps] => 6
    [hpoints] => 0
    [lpoints] => -12
    [hmoves] => 11
    [lmoves] => 0
    [hpasses] => 1
    [lpasses] => 0
    [hswaps] => 3
    [lswaps] => 0
    [today] => 2013-04-04
    [tdpoints_temp] => -24
    [tdpoints] => -24
    [htdpoints] => 0
    [ltdpoints] => -12
)

然后我将数组分成如下所示的单独值:

foreach($userStats as $singleStat){
    $played = $singleStat['played'];
    $random = $singleStat['random'];
    $win = $singleStat['win'];
    $draws = $singleStat['draws'];
    $passes = $singleStat['passes'];
    $swaps = $singleStat['swaps'];
    $hpoints = $singleStat['hpoints'];
    $lpoints = $singleStat['lpoints'];
    $hmoves = $singleStat['hmoves'];
    $lmoves = $singleStat['lmoves'];
    $hpasses = $singleStat['hpasses'];
    $lpasses = $singleStat['lpasses'];
    $hswaps = $singleStat['hswaps'];
    $lswaps = $singleStat['lswaps'];
    $today = $singleStat['today'];
    $tdpoints_temp = $singleStat['tdpoints_temp']-$playeridPoints;
    $tdpoints = $singleStat['tdpoints']-$playeridPoints;
    $htdpoints = $singleStat['htdpoints'];
    $ltdpoints = $singleStat['ltdpoints'];
} 

没问题,除了。当我回应它们时,所有变量($ random,$ hpoints,$ lmoves,$ lpasses,$ lswaps和$ htdpoints)都是“ - ”?我无法弄清楚为什么,我希望他们按原样返回“0”。

任何帮助都表示赞赏,并提前感谢: - )

// ###########编辑############# //

以下是完整代码:

if($cacheAvailable == true){ // WE GOT A CACHE
    //$memcache->delete('stats_'.$opponent);
    $userStats = $memcache->get('stats_'.$opponent);

    echo '<pre>';
    print_r($userStats);
    echo '</pre>';

    foreach($userStats as $singleStat){
        $played = $singleStat['played'];
        $random = $singleStat['random'];
        $win = $singleStat['win'];
        $draws = $singleStat['draws'];
        $passes = $singleStat['passes'];
        $swaps = $singleStat['swaps'];
        $hpoints = $singleStat['hpoints'];
        $lpoints = $singleStat['lpoints'];
        $hmoves = $singleStat['hmoves'];
        $lmoves = $singleStat['lmoves'];
        $hpasses = $singleStat['hpasses'];
        $lpasses = $singleStat['lpasses'];
        $hswaps = $singleStat['hswaps'];
        $lswaps = (int)$singleStat['lswaps']; 
        $today = $singleStat['today'];
        $tdpoints_temp = $singleStat['tdpoints_temp']-$playeridPoints;
        $tdpoints = $singleStat['tdpoints']-$playeridPoints;
        $htdpoints = $singleStat['htdpoints'];
        $ltdpoints = $singleStat['ltdpoints'];
    }
    echo 'This swap: '.$lswaps.'<br><br>';
}

1 个答案:

答案 0 :(得分:2)

0 cantbe转换为' - '必须进行一些转换

尝试使用类型转换

$played = (int)$singleStat['played'];
$random = (int)$singleStat['random'];