简体   繁体   中英

Symfony2/PHP - Issue with array

I am trying to move some code into Symfony and for some reason something is not working and I cant work out why. I have broken my function down so I can identify the issue, infact taken it to the beginning.

So I have

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'];
        var_dump($alertId);
        $alertsArray[$alertId] = array();
    }
    var_dump($alertsArray);

    return $alertsArray;
}

Now I know I have 2 alerts in my database, and this is further shown when I var_dump $alertId

int(1) int(2)

So these ids are supposed to be the first element of my array, which is what I do. But in the var_dump at the end where I output the array, I get an empty array

array(2) { 
    [1]=> array(0) { } 
    [2]=> array(0) { } 
} 

Why would this be?

Thanks

you explicitly set an empty array as the value

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

sound like you want sth like: +

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM