开发者

PHP multidimensional array convert to forms

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.00开发者_运维百科00"
    ["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.


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


foreach ($array as $i => $product) {
    foreach ($product as $key => $value) {
           $name = $key . $i;
           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\" />";
    }
}


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜