0

Here's a snippit of code I am working with:

while(($response = $responses->fetchObject()) || ($mapping = $mappings->fetchObject())) {
    $mapping = $mapping ? array('data' => array('#type'=>'link','#title' =>$mapping->title, '#href' => 'node/' . $mapping->nid)) : '';        
    $response = $response ? array('data' => array('#type'=>'link','#title' =>$response->title, '#href' => 'node/' . $response->nid)) : '';  
}

Because PHP does short circuit evaluation, $mapping doesn't get assigned anything if $response does.

How can I write this so that both $response and $mapping are assigned something in the while loop?

4

1 回答 1

2

这是我现在的解决方案:

while(true) {
    $response = $responses->fetchObject();
    $mapping = $mappings->fetchObject();

    if(!$response && !$mapping) {
        break;
    }
}

但我觉得一定有更好的方法?

于 2013-07-29T06:23:18.100 回答