开发者

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 as $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 like 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 is that in return $arr1[0] and $arr1[1] are hardcoded. And if in loop i have more than 2 arrays, lets say 0,1,2,3 ans so on, this code won't work. Any idea? Maybe my logic is compleatly wrong...


There's no need to create arrays in your loop - just add new keys directly to the 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 as $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;
}


I would probably do something like

$count = count($arr1);
for($i=0;$i<$count;$i++){
     $arr = array_merge($arr,$arr1[$i]);
}
return $arr;


I hope, I understood, what you mean ^^

foreach ($i = 0, $n = count($arr1); $i < $n; $i++) {
  $arr = array_merge($arr, $arr1[$i]);
}
return $arr;


You could do the merge in every iteration:

foreach($this->items as $item){
    $temp_arr = array(
        'item_number_'.$cpt.'' => $item['item_id'],
        'item_name_'.$cpt.'' => $item['item_name'],
        'quantity_'.$cpt.'' => $item['item_q'],
        'amount_'.$cpt.'' => $item['item_price']
    );
    $arr = array_merge($arr,$temp_arr)
    $cpt++;
}

which has the advantage that you could possibly get $temp_arr from a function,

or just add all the elements to one array:

foreach($this->items as $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++;
}


do this

$count = count($data);
    $sum = 1;
    $arr = [];
    for($i=0;$i<$count;$i++){
        $temp = $arr;
        if($i == $count - 1){
            $sum = 0;
        }
        $arr = array_merge($temp,$data[$i + $sum]);
    }
    return $arr;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜