PHP:在循环中合并数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4330583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:42:02  来源:igfitidea点击:

PHP: Merge arrays in loop

phparraysloopsmerge

提问by Milen

   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...

问题是作为回报 $arr1[0] 和 $arr1[1] 是硬编码的。如果在循环中我有 2 个以上的数组,比如说 0,1,2,3 等等,这段代码将不起作用。任何的想法?也许我的逻辑是完全错误的......

回答by Cal

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

回答by eagle12

I would probably do something like

我可能会做类似的事情

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

回答by KingCrunch

I hope, I understood, what you mean ^^

我希望,我明白你的意思^^

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

回答by AndreKR

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_arrfrom a function,

它具有您可能$temp_arr从函数中获得的优势,

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++;
}

回答by Ali sadikin

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;