Jump to content

[SOLVED] array_combine() help


djones

Recommended Posts

I need a little help getting the form values combined into an array. I have two fields:

 

<input type="text" name="qty[]" id="qty_'.$i.'" class="qty_input" value="'.$qty.'" />
<input type="hidden" name="cart_id[]" id="cart_id_'.$i.'" value="'.$cart_id.'" />

 

And I need to combine them into an array so I make sure I update the correct quantity with the cart id:

 

$array_qty = array($_POST['qty']);
$array_cart_id = array($_POST['cart_id']);

$qty_cart = array_combine($array_qty, $array_cart_id);

foreach($qty_cart as $qty_u => $cart_id_u){

// DO my update SQL here...

}

print_r($qty_cart);

 

It's not combining them. Any help?

Link to comment
https://forums.phpfreaks.com/topic/124796-solved-array_combine-help/
Share on other sites

And put the id as the key in the combined array

 

$qty_cart = array_combine($array_cart_id, $array_qty);

 

(The key must be unique so if qty is the key and there are several values of, say, 1, then you lose all but one of them.)

 

Are both fields being posted correctly? Parameter 1, that is now the $array_cart_id right?

 

Do a print_r on it and see if it is returning an array.

 

print_r($array_cart_id);

 

It could be that your field isn't populating correctly.

Hmmm I did what you mentioned and nothing prints:

 

$array_qty = $_POST['qty'];
$array_cart_id = $_POST['cart_id'];

//$qty_cart = array_combine($array_cart_id, $array_qty);

//print_r($qty_cart);
print_r($array_qty);

 

Here are some actual field examples, so there is data there:

 

<input type="text" name="qty[]" id="qty_2" class="qty_input" value="2" />
<input type="hidden" name="cart_id[]" id="cart_id_2" value="116" />

<input type="text" name="qty[]" id="qty_3" class="qty_input" value="1" />
<input type="hidden" name="cart_id[]" id="cart_id_3" value="117" />

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.