0
如果我只在第一次循環foreach循環時進入if和獲取變量值,所以下次它仍舊有舊值。爲什麼?爲什麼只有在第一個循環時纔會進入foreach?
如果我print_r($i)
並殺死它響應: A0A0A0A0A0A0A0A0A0A0A0A0A0A0TXTTXT16
真正量是它的(16)結束。所以這意味着它通過循環循環16次,但是如果僅在更改的第一個循環中進入。因此,$type
每次運行循環時都不會獲得新值。
代碼,我有:
$recordTypes = array('CnameRecord', 'ARecord', 'MxRecord', 'TxtRecord');
foreach ($recordTypes as $type) {
$modelName = $type::model()->tableName();
$record = $type::model()->findAllbySql("SELECT * FROM $modelName WHERE vhost_id LIKE $id");
if ($record) {
foreach ($record as $key=>$rec) {
if ($type == 'ARecord'){
$type = 'A' + $i;
$hostname = $rec['sub_vhost_name'];
$points_to = $rec['points_to'];
$ttl = $rec['ttl'];
} else if ($type == 'CnameRecord') {
$type = 'CNAME';
$hostname = $rec['hostname'];
$points_to = $rec['points_to'];
$ttl = $rec['ttl'];
} else if ($type == 'MxRecord') {
$type = 'MX';
$priority = $rec['priority'] . ' ';
$points_to = $rec['points_to'];
$ttl = $rec['ttl'];
$hostname = $rec['host'];
} else if ($type == 'TxtRecord') {
$type = 'TXT';
$hostname = $rec['hostname'];
$points_to = '"' . $rec['txt_value'] . '" ';
$ttl = $rec['ttl'];
}
$i++;
print_r($type);
$custom_content = $hostname . ' ' . $ttl . ' ' . 'IN' . ' ' . $type . ' ' . $priority . $points_to . "\n";
}
}
}
print_r($i);die;
迭代器'$ i'初始化必須在foreach循環之外 –