简体   繁体   中英

Using an array value to retrieve from another array

I have created an array, one of the is intended to be a string used by php to display a field from a record retrieved from sqlite3.

My problem is that ... it doesn't.

The array is defined, "1" being the first database field, and "2" is the second database field:

EDIT : I have re-defined the problem as a script so you can see the whole thing:

//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 values\n";
foreach($GLOBALS["fields"] as $field)
    {
        $label = $field['form_label'];
        $it = $field['display'];    
        $line = "\"$label = \" . $it .\"\n\"";
        print $line;
    }

Output:

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

Don't call variable from string. Just concatenate it :

foreach($GLOBALS["fields"] as $field){
    $label = $field['form_label'];
    $it = $field['display'];

    eval("$it = ".$it);
    $line = $label." = ".$it."\n";
    print $line;
}

Well, how do it looks ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM