1

我有一个数组($vEvents)如下:

Array
(
[0] => Array
    (
        [id] => 1174
        [date] => 120411
        [name] => DR. J 'Souled Out' ~ 9pm (No Cover)
        [pubId] => 44
        [price] => 
    )

[1] => Array
    (
        [id] => 1108
        [date] => 120410
        [name] => Verb presents Open Stage ~ 9pm (No Cover)
        [pubId] => 44
        [price] => 
    )

[2] => Array
    (
        [id] => 1104
        [date] => 120409
        [name] => DR. J 'Souled Out' ~ 9pm (No Cover)
        [pubId] => 44
        [price] => 
    )
[currLimit] => 10
[eventsRemaining] => 4

我在屏幕上回显它:

reset ($vEvents);
foreach ($vEvents as $key => &$value) {
    if ($key != 'currLimit' && $key != 'eventsRemaining'){ //Makes sure it doesn't print out the limiter value
    $m = substr($value['date'],0,2);
    $d = substr($value['date'],2,2);
    $y = '20'.substr($value['date'],4,2);
    echo date("D", mktime(0, 0, 0, $m, $d, $y)).", ".date("M", mktime(0, 0, 0, $m, $d, $y))." ".$d.", ".$y." - ".$value['name'].'<br />';
    }
}

但是在我的输出中,它没有得到所有显示的数组条目,而是一直跳过第一个......输出:

Sat, Dec 04, 2010 - Verb presents Open Stage ~ 9pm (No Cover)
Fri, Dec 04, 2009 - DR. J 'Souled Out' ~ 9pm (No Cover)

但是,我的预期输出应该是:

Sun, Dec 04, 2011 - DR. J 'Souled Out' ~ 9pm (No Cover)
Sat, Dec 04, 2010 - Verb presents Open Stage ~ 9pm (No Cover)
Fri, Dec 04, 2009 - DR. J 'Souled Out' ~ 9pm (No Cover)

我曾尝试更改name键中的值等,但所有其他整个显示都很好,它总是跳过第一个键。我以前从未遇到过这个问题。想法?

当我注释掉:if ($key != 'currLimit' && $key != 'eventsRemaining'){ //Makes sure it doesn't print out the limiter value行时,它工作正常。第一个元素的键为零是否相关?但是为什么我的if检查会失败呢?

4

2 回答 2

2

PHP 在处理 0 的值时很奇怪……在大多数情况下,它会将它们视为 false。所以,

if ($key != 'currLimit' && $key != 'eventsRemaining')

当 $key == 0 时将评估为 false。我将其更改为:

if ($key !== 'currLimit' && $key !== 'eventsRemaining')
于 2012-05-21T18:11:14.947 回答
-3

因为这是一个数组,我会尝试使用不同类型的循环,它应该会给你预期的结果:

<?php

$length = count( $vEvents );

for( $i = 0; $i < $length; i ++ )
{

$key = $i;
$value = $vEvents[ $i ];

//rest of the loop code here

}
?>
于 2012-05-21T17:47:18.343 回答