5

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.

2
  • 4
    It's really just a matter of echo-ing the contents of the array in a for loop. Commented May 6, 2011 at 18:52
  • 1
    The question is no way idiotic.I have been planning to ask this question myself. Commented May 6, 2011 at 19:15

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

1 Comment

Technically, they also want product_name -> product#, product_qty -> qty#, and product_price -> amount#.
2
foreach ($array as $i => $product) {
    foreach ($product as $key => $value) {
           $name = $key . $i;
           echo "<input type='hidden' name='$name' value='$value' />";
    }
}

Comments

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

Comments

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

2 Comments

why are you mixing for and foreach? why not do 2 foreach loops?
To get the counter, didn't know you could use foreach => with a non-associative array.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.