2013-10-29 40 views
0

我正在爲Zen Cart的模塊編寫一些代碼。 $ stores_id是含3個值的數組:for循環沒有迴應我的數組中的期望值

$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]"> 

誰能告訴我我做錯了什麼?

回答

0

它看起來像你嵌套你的第二個參數1深度太遠 -

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

所以當$i == 0,你得到$stores_id[1],這是2,而不是$stores_id[0]這是1。當你到達$i == 2時,你有$stores_id[3]這不在陣列中。

所以,要麼去除外層陣列 -

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

或從內陣列減去1返回值

htmlspecialchars(stripslashes($stores_id[$stores_id[$i]['stores_id']-1]) 
+0

謝謝你,那確實起作用。我使用了第一個選項,因爲在正常的操作中,數組可以用任意值的組合填充,然後第二個選項不起作用。 – Zen4All

+0

通過刪除'['stores_id']'來進一步簡化代碼。 現在回顯線爲: 'echo zen_draw_hidden_​​field('stores_id ['。$ stores_id [$ i]。']',htmlspecialchars(stripslashes($ stores_id [$ i]),ENT_COMPAT,CHARSET,TRUE));'' – Zen4All