Symfony2 - 被覆盖的数组

时间:2015-02-23 15:43:30

标签: php arrays symfony

我的数组有点问题。我基本上声明了一个数组

public function getAvailabilityData(){
    $alerts = $this->em->getRepository('NickAlertBundle:AvailabilityAlert')->getActiveAlertIds();

    $alertsArray = array();

    if (!$alerts) {
        throw $this->createNotFoundException('Unable to find Availability.');
    }

    foreach($alerts as $alert){
        $alertId = (int)$alert['id'];
        $alertsArray[$alertId] = array();

        $allAvailability = $this->em->getRepository('NickAlertBundle:AvailabilityAlertAvailability')->getAlertAvailability($alertId);

        foreach($allAvailability as $alertAvailability)
        {
            var_dump($alertAvailability);
            $dateString = $alertAvailability['lastUpdated'];
            $dateString = $dateString->format('d M Y H:00');

            // Create the second dimension of the array, using the alert pseudo as the key.
            if (empty($alerts[$alertId][$alertAvailability['alertPseudo']])) {
                $alertsArray[$alertId][$alertAvailability['alertPseudo']] = array();
            }

            // Create the third dimension of the array, using the flight number as the key.
            if (empty($alerts[$alertId][$alertAvailability['alertPseudo']][$alertAvailability['flightNumber']])) {
                $alertsArray[$alertId][$alertAvailability['alertPseudo']][$alertAvailability['flightNumber']] = array();
            }

            // Create the fourth dimension of the array, using the date string as the key.
            if (empty($alerts[$alertId][$alertAvailability['alertPseudo']][$alertAvailability['flightNumber']][$dateString])) {
                $alertsArray[$alertId][$alertAvailability['alertPseudo']][$alertAvailability['flightNumber']][$dateString] = array();
            }

            // Create the fifth dimension of the array, using the class letter as a key, and the availability value as the value.
            $alertsArray[$alertId][$alertAvailability['alertPseudo']][$alertAvailability['flightNumber']][$dateString][$alertAvailability['classLetter']] = $alertAvailability['availability'];
        }
    }

    var_dump($alertsArray);
    return $alertsArray;
}

现在第一个var_dump告诉我所有数据都存在。它就是这样出来的

array(5) { 
    ["availability"]=> string(1) "9" 
        ["lastUpdated"]=> object(DateTime)#378 (3) { 
            ["date"]=> string(19) "2015-02-22 14:28:58" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/London"
        } 
        ["classLetter"]=> string(1) "C" 
        ["alertPseudo"]=> string(3) "123" 
        ["flightNumber"]=> string(3) "VS7" 
} 

array(5) { 
    ["availability"]=> string(1) "9" 
        ["lastUpdated"]=> object(DateTime)#369 (3) { 
            ["date"]=> string(19) "2015-02-22 14:28:58" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/London" 
        } 
        ["classLetter"]=> string(1) "D" 
        ["alertPseudo"]=> string(3) "123" 
        ["flightNumber"]=> string(3) "VS7" 
} 

array(5) { 
    ["availability"]=> string(1) "9" 
        ["lastUpdated"]=> object(DateTime)#367 (3) { 
            ["date"]=> string(19) "2015-02-22 14:28:58" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/London" 
        } 
        ["classLetter"]=> string(1) "I" 
        ["alertPseudo"]=> string(3) "123" 
        ["flightNumber"]=> string(3) "VS7" 
}

现在我在创建我的数组,它应该为上面输出的每个“行”创建记录,在这种情况下它应该做3.但是,如果我输出数组,我得到

array(1) { 
    [1]=> array(1) { 
        ["123"]=> array(1) { 
            ["VS7"]=> array(1) { 
                ["22 Feb 2015 14:00"]=> array(1) { 
                    ["I"]=> string(1) "9" 
                } 
            } 
        } 
    } 
} 

所以它只是从我的var_dump创建最后一个Object,看起来它覆盖了另一个2.

现在这与我工作的普通PHP版本完全一样,唯一的区别是我做的不是foreach循环

while ($aRow = $availabilitySql->fetch(PDO::FETCH_ASSOC)) {

那么为什么我的数组创建每次都超越?

由于

1 个答案:

答案 0 :(得分:-1)

我无法看到$ alertId的定义位置以及下一行应该如何更改。我认为它不会改变你一直使用相同的行。

你也检查if(空($ alerts [$ alertId] ...但是数组叫$ alertsArray所以我认为你的意思是if(空($ alertsArray [$ alertId] ...