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

5 Answers 5

6

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah ... i'm so !#$$%. Maybe i need to go to sleep... :-) .. Anyway .. thanks a lot!
3

I would probably do something like

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

2 Comments

hey bro i have same problem but what is $arr1 ?
ok got it. but i have one array just like $arr1 and i have to merge $arry[0] to $arry[1] how to do this ?
2

I hope, I understood, what you mean ^^

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

Comments

0

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

Comments

0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.