user2528939
user2528939

Reputation: 51

post two dimensional array in php

I'm trying to post and receive a two dimensional array, but I can't get it to work.

Could you guys help me out?

Thanks in advance!

Here is my code:

$items[] = array(
  'pid' => $pid
  , 'qty' => $product_qty
);    

<input type="hidden" name="items[]" id="pid" />

foreach ($_POST['items'] as $row) {
  $pid = $row['pid'];
  $product_qty = $row['qty'];
}

Upvotes: 2

Views: 1154

Answers (2)

fusion3k
fusion3k

Reputation: 11689

Change your code in a way like this:

$items = array('pid' => $pid, 'qty' => $product_qty);
foreach( $items as $key => $val )
{
    echo '<input type="hidden" name="items['.$key.']" value="'.$val.'" id="'.$key.'" />';
}

In your original code, $items[] add a new item to array $items.

Also, HTML doesn't interpret php variables, so your <input name="items[]" will produce $_POST[items][0] with an empty value.

Upvotes: 3

oliver
oliver

Reputation: 2843

It's as simple as this:

$myarr = array( 'pid' => array($pid), 'qty' => array($product_qty)); 

Upvotes: 0

Related Questions