在我的文件中,我编写了以下代码:
if ( is_array( $form_data['list'][95] ) ) {
$i = 1;
foreach ( $form_data['list'][95] as $row ) {
/* Uses the column names as array keys */
$name[$i] = $row['Name'];
$phonetic[$i] = $row['Phonetic Spelling'];
if ($phonetic[$i] == ''){$spelling[$i] = '';} else {$spelling[$i] = '('.$phonetic[$i].')';}
$order[$i] = $row['Order'];
$full_row[$i] = $order[$i].' - '.$name[$i].' '.$spelling[$i];
$i++;
}
rsort($full_row);
foreach ($full_row as $key => $val) {
echo "$val<br />";
}
}
这很好用。它输出我期望的列表。但是,如果我尝试将其作为函数输出,则不会发生任何事情。
function OrderFormatIntros(){
if ( is_array( $form_data['list'][95] ) ) {
$i = 1;
foreach ( $form_data['list'][95] as $row ) {
/* Uses the column names as array keys */
$name[$i] = $row['Name'];
$phonetic[$i] = $row['Phonetic Spelling'];
if ($phonetic[$i] == ''){$spelling[$i] = '';} else {$spelling[$i] = '('.$phonetic[$i].')';}
$order[$i] = $row['Order'];
$full_row[$i] = $order[$i].' - '.$name[$i].' '.$spelling[$i];
$i++;
}
rsort($full_row);
foreach ($full_row as $key => $val) {
echo "$val<br />";
}
}
}
OrderFormatIntros();
我需要提供更多解释吗?或者有一个明确的原因,为什么代码在作为函数调用时不会输出?
答案 0 :(得分:2)
OrderFormatIntros函数中的代码与$ form_data变量的内容不相关;您必须将其传递给函数,例如:
<?php
function OrderFormatIntros($form_data){
if ( is_array( $form_data['list'][95] ) ) {
$i = 1;
foreach ( $form_data['list'][95] as $row ) {
/* Uses the column names as array keys */
$name[$i] = $row['Name'];
$phonetic[$i] = $row['Phonetic Spelling'];
if ($phonetic[$i] == ''){$spelling[$i] = '';} else {$spelling[$i] = '('.$phonetic[$i].')';}
$order[$i] = $row['Order'];
$full_row[$i] = $order[$i].' - '.$name[$i].' '.$spelling[$i];
$i++;
}
rsort($full_row);
foreach ($full_row as $key => $val) {
echo "$val<br />";
}
}
}
OrderFormatIntros($form_data);