0

在“foreach”循环中引用变量时遇到一些问题。

这是我的 CakePHP 代码片段: http: //pastebin.com/s7qtYahA

第一个代码为相同数量的内部“foreach”循环迭代分配双重分配。

但是第二个代码一切正常。谁能澄清一下扭曲..?提前致谢。:)

4

1 回答 1

0

因为 $values 已使用 array() 重置/清除

<?php
foreach($fields as &$field){
        $fieldValues = $this->controller->DomainFormFieldValue->find('all', array('conditions' => array('domain_form_field_id' => $field['DomainFormField']['id'])));
        foreach($fieldValues as $fieldValue){
                $field['DomainFormFieldValue'][] = $fieldValue['DomainFormFieldValue'];
        }
}
unset($field);
?>

2nd code:
<?php
foreach($fields as &$field){
        $fieldValues = $this->controller->DomainFormFieldValue->find('all', array('conditions' => array('domain_form_field_id' => $field['DomainFormField']['id'])));

        $values = array(); /* Array has been resetted */ 

        foreach($fieldValues as $fieldValue){
                $values[] = $fieldValue['DomainFormFieldValue'];
        }
        $field['DomainFormFieldValue'] = $values;
}
unset($field);
?>
于 2012-11-01T05:17:19.817 回答