0

我正在为 Zen Cart 的模块编写一些代码。$stores_id 是一个包含 3 个值的数组:

$stores_id[0]="1";
$stores_id[1]="2";
$stores_id[2]="3";

使用以下代码,我试图回显一个隐藏的输入字段,其中填充了数组中的数据

for ($i=0, $n=sizeof($stores_id); $i<$n; $i++)
{
  echo zen_draw_hidden_field('stores_id['. $stores_id[$i]['stores_id'] .']', htmlspecialchars(stripslashes($stores_id[$stores_id[$i]['stores_id']]), ENT_COMPAT, CHARSET, TRUE));
}

回显的结果是:

<input type="hidden" value="2" name="stores_id[1]">
<input type="hidden" value="3" name="stores_id[2]">
<input type="hidden" name="stores_id[3]">

虽然我期望它是:

<input type="hidden" value="1" name="stores_id[1]">
<input type="hidden" value="2" name="stores_id[2]">
<input type="hidden" value="3" name="stores_id[3]">

谁能告诉我我做错了什么?

4

1 回答 1

0

It looks like you are nesting your 2nd parameter 1 depth too far -

$stores_id[$stores_id[$i]['stores_id']]

So when $i == 0, you are getting $stores_id[1], which is 2, instead of $stores_id[0] which is 1. And when you get to $i == 2 you have $stores_id[3] which is not in the array.

So either remove the outer array -

htmlspecialchars(stripslashes($stores_id[$i]['stores_id'])

or subtract 1 from the inner array returned value

htmlspecialchars(stripslashes($stores_id[$stores_id[$i]['stores_id']-1])
于 2013-10-29T15:56:47.903 回答