带有关联数组的PHP str_replace

时间:2013-04-19 17:51:56

标签: php

我有从DB返回的行,我需要将cols中使用的特定标记字符更改为显示的右侧字符:

$row = mysqli_fetch_array($res, MYSQLI_ASSOC); //the result of query is 1 row

$markup = array("@", "#", "&", "$"); // this is our markup
$meaning   = array("↑", "↓", "(dil.)", "(conc.)"); // this is our meaning of the markup for the display

foreach ($row as $k => $v) { //for each element of the array we change one char for the other
$v = str_replace ($markup, $meaning, $v);
echo "<br>$v";

}
UPD现在越来越奇怪了。如果更改之前的str_replace是对数组进行跟踪,我做了一个错误,我修复了它,ant跟踪显示它有效!但是这个数组的特定输出在下面,它仍然出现在未替换的字符中!

    $reagent_no = "reaction_l" . $i; // determining the column number, irrelevant to the issue
    echo "<td>\n<h3>{$row[$reagent_no]}</h3></td>"; //output of the supposedly 
//changed characters, but it still outputs the markup, as if i didnt do anything to the array previously

2 个答案:

答案 0 :(得分:1)

您首先打印变量,然后使用str_replace()函数。将其更改为: -

foreach ($row as $k => $v) { //for each element of the array we change one char for the other
$v = str_replace ($markup, $meaning, $v);
echo "<br>$v";
}

答案 1 :(得分:1)

问题是str_replaceecho之后执行。

尝试:

foreach ($row as $k => $v) { //for each element of the array we change one char for the other
    $v = str_replace ($markup, $meaning, $v);
    echo "<br>$v";
}