使用要从另一个数组检索的数组值



我已经创建了一个数组,其中一个是php用来显示从sqlite3检索到的记录字段的字符串。

我的问题是……它不是。

数组被定义,"1"是第一个数据库字段,"2"是第二个数据库字段:

编辑:我已经重新定义了这个问题作为一个脚本,所以你可以看到整个事情:

//If I have an array (simulating a record retrieved from database):
$record = array(
    name => 'Joe',
    comments => 'Good Bloke',
    );
//then I define an array to reference it:
$fields = array( 
    1 => array(
    'db_index' => 'name',
    'db_type' => 'TEXT',
    'display' => '$record["name"]',
    'form_label' => 'Name',
    ),
    2 => array(
    'db_index' => 'comments',
    'db_type' => 'TEXT',
    'display' => '$record["comments"]',
    'form_label' => 'Comments',
    ),  
);  
//If I use the lines:
print "expected output:n";
print " Name = " . $record["name"] ."n";
print " Comments = " . $record["comments"] ."n";
//I display the results from the $record array correctly.
//However if I try & use the fields array with something like:
Print "Output using Array valuesn";
foreach($GLOBALS["fields"] as $field)
    {
        $label = $field['form_label'];
        $it = $field['display'];    
        $line = ""$label = " . $it ."n"";
        print $line;
    }
输出:

Expected output:
 Name = Joe
 Comments = Good Bloke
Output using Array values:
 Name = $record["name"] 
 Comments = $record["comments"] 

不要从字符串中调用变量。把它连接起来:

foreach($GLOBALS["fields"] as $field){
    $label = $field['form_label'];
    $it = $field['display'];
    eval("$it = ".$it);
    $line = $label." = ".$it."n";
    print $line;
}

看起来怎么样?

相关内容

  • 没有找到相关文章

最新更新