PHP 多维数组转换为表单

发布于 2024-11-05 06:20:13 字数 873 浏览 0 评论 0原文

抱歉问了这个白痴问题,因为 magento 我的脑子一片空白。

问题是:

我这里有一个数组:

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"
  }
}

我怎样才能将其转换为:

<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" />

谢谢您的回答。

Sorry for asking this idiot question, my head was blackout because of 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

十二 2024-11-12 06:20:13

试试这个:

foreach($array as $pKey=>$product){
   foreach($product as $key=>$option){
       echo "<input type='hidden' name='{$key}_{$pKey}' value='$option'/>\n";
   }
}

它会给你这样的结果:

<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'/>

这是一个演示:http://codepad.org/Eg2mejZH

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

凉宸 2024-11-12 06:20:13
foreach ($array as $i => $product) {
    foreach ($product as $key => $value) {
           $name = $key . $i;
           echo "<input type='hidden' name='$name' value='$value' />";
    }
}
foreach ($array as $i => $product) {
    foreach ($product as $key => $value) {
           $name = $key . $i;
           echo "<input type='hidden' name='$name' value='$value' />";
    }
}
止于盛夏 2024-11-12 06:20:13

即使是简单的也应该包括适当的消毒。我们能举出的例子越多,说明如何正确地做到这一点,我们所有人的生活就越好。

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\" />";
    }
}

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\" />";
    }
}
抱着落日 2024-11-12 06:20:13
$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);
    }
}

会给你

<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'/>
$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'/>
夜吻♂芭芘 2024-11-12 06:20:13

尝试这样:
转换成 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; ?>

你好!

添加完整代码

$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; ?>

try this way:
into HTML code

<?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; ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文