PHP: Merge arrays in loop -
public function getcheckoutform(){ $arr = array( 'cmd' => '_cart', 'business' => 'some@mail', 'no_shipping' => '1', 'upload' => '1', 'return' => 'url', 'cancel_return' => 'url1', 'no_note' => '1', 'currency_code' => 'url2', 'bn' => 'pp-buynowbf'); $cpt=1; foreach($this->items $item){ $arr1[] = array( 'item_number_'.$cpt.'' => $item['item_id'], 'item_name_'.$cpt.'' => $item['item_name'], 'quantity_'.$cpt.'' => $item['item_q'], 'amount_'.$cpt.'' => $item['item_price'] ); $cpt++; } return array_merge($arr,$arr1[0],$arr1[1]); }
this returns array that:
array ( [cmd] => _cart [business] => some@mail [no_shipping] => 1 [upload] => 1 [return] => url1 [cancel_return] =>url2 [no_note] => 1 [currency_code] => eur [bn] => pp-buynowbf [item_number_1] => 28 [item_name_1] => item_name_1 [quantity_1] => 1 [amount_1] => 5 [item_number_2] => 27 [item_name_2] => item_name_2 [quantity_2] => 1 [amount_2] => 30 )
the problem in return $arr1[0] , $arr1[1] hardcoded. , if in loop have more 2 arrays, lets 0,1,2,3 ans on, code won't work. idea? maybe logic compleatly wrong...
there's no need create arrays in loop - add new keys directly first array:
public function getcheckoutform(){ $arr = array( 'cmd' => '_cart', 'business' => 'some@mail', 'no_shipping' => '1', 'upload' => '1', 'return' => 'url', 'cancel_return' => 'url1', 'no_note' => '1', 'currency_code' => 'url2', 'bn' => 'pp-buynowbf' ); $cpt=1; foreach($this->items $item){ $arr['item_number_'.$cpt] = $item['item_id']; $arr['item_name_'.$cpt] = $item['item_name']; $arr['quantity_'.$cpt] = $item['item_q']; $arr['amount_'.$cpt] = $item['item_price']; $cpt++; } return $arr; }
Comments
Post a Comment