1

Is there anyway to loop through an array and insert each instance into another array?

   $aFormData = array(
        'x_show_form' => 'PAYMENT_FORM',
        foreach($aCartInfo['items'] as $item){
            'x_line_item' => $item['id'].'<|>'.$item['title'].'<|>'.$item['description'].'<|>'.$item['quantity'].'<|>'.$item['price'].'<|>N',
        }

    );

3 Answers 3

2

Not directly in the array declaration, but you could do the following:

$aFormData = array(
    'x_show_form' => 'PAYMENT_FORM',
    'x_line_item' => array(),
);
foreach($aCartInfo['items'] as $item){
    $aFormData['x_line_item'][] = $item['id'].'<|>'.$item['title'].'<|>'.$item['description'].'<|>'.$item['quantity'].'<|>'.$item['price'].'<|>N';
}
Sign up to request clarification or add additional context in comments.

Comments

2

Yes, there is a way ; but you must go in two steps :

  • First, create your array with the static data
  • And, then, dynamically add more data

In your case, you'd use something like this, I suppose :
$aFormData = array(
   'x_show_form' => 'PAYMENT_FORM',
   'x_line_item' => array(),  // right now, initialize this item to an empty array
);

foreach($aCartInfo['items'] as $item){
    // Add the dynamic value based on the current item
    // at the end of $aFormData['x_line_item']
    $aFormData['x_line_item'][] = $item['id'] . '<|>' . $item['title'] . '<|>' . $item['description'] . '<|>' . $item['quantity'] . '<|>' . $item['price'] . '<|>N';
}

And, of course, you should read the [section of the manual that deals with **arrays**][1] : it'll probably help you a lot ;-)

Comments

0

You mean something like this :

$aFormData = array(
    'x_show_form' => 'PAYMENT_FORM',
    'x_line_item' => array(),
);

foreach($aCartInfo['items'] as $item) {
     $aFormData['x_line_item'][] = implode('<|>', $aCartInfo['items']).'<|>N',
}

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.