2011-05-06 27 views
5

Perdón por hacer esta pregunta idiota, mi cabeza se apagó debido a magento.matriz PHP multidimensional convertir a formularios

Aquí está el problema:

que tengo aquí una matriz:

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

¿Cómo puedo hacer esto a transformar:

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

Gracias por su respuesta.

+4

no deja de ser una cuestión de eco-ción de los contenidos de la matriz en un bucle. – gd1

+1

La pregunta no es de ninguna manera idiota. He estado planeando hacer esta pregunta yo mismo. – Gandalf

Respuesta

5

Prueba esto:

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

que le dará un resultado como este:

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

Aquí es una demostración: http://codepad.org/Eg2mejZH

+0

Técnicamente, también quieren product_name -> product #, product_qty -> qty #, y product_price -> amount #. – mellamokb

+0

@mellamokb hecho! – Neal

2
foreach ($array as $i => $product) { 
    foreach ($product as $key => $value) { 
      $name = $key . $i; 
      echo "<input type='hidden' name='$name' value='$value' />"; 
    } 
} 
0

Incluso los más fáciles deben incluir adecuado desinfectar Cuantos más ejemplos podamos poner de cómo hacer esto bien, mejor estamos todos.

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

le dará

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

¿por qué mezclas 'for' y' foreach'? ¿Por qué no hacer 2 bucles 'foreach'? – Neal

+0

Para obtener el contador, no sabía que podía usar foreach => con una matriz no asociativa. – jamaicahest

0

tratar de esta manera: en el código 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; ?> 

Saludos!

Añadir un código completo

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