Sorry for asking this idiot question, my head was blackout because of magento.
对于提出这个愚蠢的问题很抱歉,因为magento,我的头被停电了。
Here is the problem:
这是问题所在:
I have here an array:
我这里有一个数组:
array(2) {
[0]=>
array(3) {
["product_name"]=>
string(12) "Test Product"
["product_qty"]=>
string(6) "2.0000"
["product_price"]=>
string(7) "15.0000"
}
[1]=>
array(3) {
["product_name"]=>
string(6) "Test 2"
["product_qty"]=>
string(6) "3.0000"
["product_price"]=>
string(7) "25.0000"
}
}
How can I make transform this to:
我怎样才能将其转换为:
<input type="hidden" name="product1" value="Test Product" />
<input type="hidden" name="amount1" value="2" />
<input type="hidden" name="qty1" value="15" />
<input type="hidden" name="product2" value="Test 2" />
<input type="hidden" name="amount2" value="3" />
<input type="hidden" name="qty2" value="25" />
Thanks for your answer.
感谢您的回答。
5 个解决方案
#1
5
Try this:
foreach($array as $pKey=>$product){
foreach($product as $key=>$option){
echo "<input type='hidden' name='{$key}_{$pKey}' value='$option'/>\n";
}
}
It will give you a result like this:
它会给你一个这样的结果:
<input type='hidden' name='product_name_0' value='Test Product'/>
<input type='hidden' name='product_qty_0' value='2.0000'/>
<input type='hidden' name='product_price_0' value='15.0000'/>
<input type='hidden' name='product_name_1' value='Test 2'/>
<input type='hidden' name='product_qty_1' value='3.0000'/>
<input type='hidden' name='product_price_1' value='25.0000'/>
Here is a demo: http://codepad.org/Eg2mejZH
这是一个演示:http://codepad.org/Eg2mejZH
#2
2
foreach ($array as $i => $product) {
foreach ($product as $key => $value) {
$name = $key . $i;
echo "<input type='hidden' name='$name' value='$value' />";
}
}
#3
0
Even the easy ones should include proper sanitizing. The more examples we can put out there of how to do this right, the better off we all are.
即使是简单的也应该包括适当的消毒。我们可以提供更多关于如何做到这一点的例子,我们所做的就越好。
foreach ($array as $i => $item) {
foreach ($item as $k => $v) {
$name = htmlspecialchars($k . ($i + 1));
$value = htmlspecialchars($v);
echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />";
}
}
#4
0
$arrayLen = count($array);
for ($i = 0;$i < $arrayLen;$i++) {
foreach ($array[$i] as $field => $value) {
printf("<input type='hidden' name='%s%d' value='%s'/>", preg_replace("/^product_/", "", $field), $i+1, $value);
}
}
will give you
会给你
<input type='hidden' name='name1' value='Test Product'/>
<input type='hidden' name='qty1' value='2.0000'/>
<input type='hidden' name='price1' value='15.0000'/>
<input type='hidden' name='name2' value='Test 2'/>
<input type='hidden' name='qty2' value='3.0000'/>
<input type='hidden' name='price2' value='25.0000'/>
#5
0
try this way: into HTML code
试试这种方式:进入HTML代码
<?php foreach ($allData as $data): $id=1; ?>
<input type="hidden" name="product<?php echo $id; ?>" value="<?php echo $data['product_name']; ?>" />
<input type="hidden" name="amount<?php echo $id; ?>" value="<?php echo $data['product_qty']; ?>" />
<input type="hidden" name="qty<?php echo $id; ?>" value="<?php echo $data['product_price']; ?>" />
<?php $id++; endforeach; ?>
Greetings!
Add Full Code
添加完整代码
$allData = array(
array(
"product_name"=> "Test Product",
"product_qty"=>"2.0000",
"product_price"=>"15.0000",
),
array(
"product_name"=>"Test 2",
"product_qty"=>"3.0000",
"product_price"=>"25.0000",
),
);
?>
<?php foreach ($allData as $data): $id=1; ?>
<input type="text" name="product<?php echo $id; ?>" value="<?php echo $data['product_name']; ?>" />
<input type="text" name="amount<?php echo $id; ?>" value="<?php echo $data['product_qty']; ?>" />
<input type="text" name="qty<?php echo $id; ?>" value="<?php echo $data['product_price']; ?>" />
<?php $id++; endforeach; ?>
#1
5
Try this:
foreach($array as $pKey=>$product){
foreach($product as $key=>$option){
echo "<input type='hidden' name='{$key}_{$pKey}' value='$option'/>\n";
}
}
It will give you a result like this:
它会给你一个这样的结果:
<input type='hidden' name='product_name_0' value='Test Product'/>
<input type='hidden' name='product_qty_0' value='2.0000'/>
<input type='hidden' name='product_price_0' value='15.0000'/>
<input type='hidden' name='product_name_1' value='Test 2'/>
<input type='hidden' name='product_qty_1' value='3.0000'/>
<input type='hidden' name='product_price_1' value='25.0000'/>
Here is a demo: http://codepad.org/Eg2mejZH
这是一个演示:http://codepad.org/Eg2mejZH
#2
2
foreach ($array as $i => $product) {
foreach ($product as $key => $value) {
$name = $key . $i;
echo "<input type='hidden' name='$name' value='$value' />";
}
}
#3
0
Even the easy ones should include proper sanitizing. The more examples we can put out there of how to do this right, the better off we all are.
即使是简单的也应该包括适当的消毒。我们可以提供更多关于如何做到这一点的例子,我们所做的就越好。
foreach ($array as $i => $item) {
foreach ($item as $k => $v) {
$name = htmlspecialchars($k . ($i + 1));
$value = htmlspecialchars($v);
echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />";
}
}
#4
0
$arrayLen = count($array);
for ($i = 0;$i < $arrayLen;$i++) {
foreach ($array[$i] as $field => $value) {
printf("<input type='hidden' name='%s%d' value='%s'/>", preg_replace("/^product_/", "", $field), $i+1, $value);
}
}
will give you
会给你
<input type='hidden' name='name1' value='Test Product'/>
<input type='hidden' name='qty1' value='2.0000'/>
<input type='hidden' name='price1' value='15.0000'/>
<input type='hidden' name='name2' value='Test 2'/>
<input type='hidden' name='qty2' value='3.0000'/>
<input type='hidden' name='price2' value='25.0000'/>
#5
0
try this way: into HTML code
试试这种方式:进入HTML代码
<?php foreach ($allData as $data): $id=1; ?>
<input type="hidden" name="product<?php echo $id; ?>" value="<?php echo $data['product_name']; ?>" />
<input type="hidden" name="amount<?php echo $id; ?>" value="<?php echo $data['product_qty']; ?>" />
<input type="hidden" name="qty<?php echo $id; ?>" value="<?php echo $data['product_price']; ?>" />
<?php $id++; endforeach; ?>
Greetings!
Add Full Code
添加完整代码
$allData = array(
array(
"product_name"=> "Test Product",
"product_qty"=>"2.0000",
"product_price"=>"15.0000",
),
array(
"product_name"=>"Test 2",
"product_qty"=>"3.0000",
"product_price"=>"25.0000",
),
);
?>
<?php foreach ($allData as $data): $id=1; ?>
<input type="text" name="product<?php echo $id; ?>" value="<?php echo $data['product_name']; ?>" />
<input type="text" name="amount<?php echo $id; ?>" value="<?php echo $data['product_qty']; ?>" />
<input type="text" name="qty<?php echo $id; ?>" value="<?php echo $data['product_price']; ?>" />
<?php $id++; endforeach; ?>