PHP How to replace string literals using a value from an array?
I'm not sure if it's the proper title to use...
Here is my question.
I have stored table field names from a database in an array, and created a string like following
foreach ($fields as $field) {
$str.="<td>{$data[key][field]}</td>";
}
And I have stored the data from the table in a different array which is of the form
$data=array(array('name'=>"name 1",'address'=>"Address One"),array('name'=>"Name 2",'address'=>"Address two"),array('name'=>"Name 3",'address'=>"address three"),array('name'=>"test",'address'=>"test one"));
Now I have done the following
eval("\$str = \"$str\";");
foreach ($data as $key => $souceStr) {
?>
<tr>
<?=$str?>
</tr>
<?
}
?>
My aim is to replace the string that i have created from the first array with the values from the second array. I have seen coding in CMS like joomla where they replace '{somename}' with a value I want to do the same here.
=================================================
I'm sorry My question was not clear. But i got what i was looking for. Here is the code
<table width="100%" cellpadding="0" cellspacing="0" 开发者_如何学Calign="center" border="1">
<tr>
<?
foreach ($header as $val) {
?>
<th><?= $val ?></th>
<?
}
?>
</tr>
<?
foreach ($fields as $field) {
$str.='<td>$souceStr['.$field.']</td>';
}
foreach ($data as $key => $souceStr) {
eval("\$eval_str = \"$str\";");
?>
<tr>
<?php echo $eval_str; ?>
</tr>
<?
}
?>
The idea was to print a html table.
I don't think I completely understand what your question is, but what if you simply made a string from the second array just like you did from the first one? the result wold be the same, right?
精彩评论