Foreach inside another array in php [duplicate]

0

How can I generate with php this array to make a payment transaction for the Pagseguro?

$params = array(
        'email'                     => $PAGSEGURO_EMAIL,  
        'token'                     => $PAGSEGURO_TOKEN,
        'creditCardToken'           => $creditCardToken,
        'senderHash'                => $senderHash,
        'receiverEmail'             => $PAGSEGURO_EMAIL,
        'paymentMode'               => 'default', 
        'paymentMethod'             => 'creditCard', 
        'currency'                  => 'BRL',
        // 'extraAmount'               => '1.00',       

        'itemId1'                   => '0001',
        'itemDescription1'          => 'PHP Test',  
        //'itemAmount1'               => $numero,  
        'itemAmount1'               => '1.00',  
        'itemQuantity1'             => 1,

        'itemId2'                   => '0002',
        'itemDescription2'          => 'PHP Test2',  
        //'itemAmount2'               => $numero,  
        'itemAmount2'               => '1.00',  
        'itemQuantity2'             => 1,           

        'reference'                 => 'REF1234',       
        'senderName'                => $senderName,
        'senderCPF'                 => $senderCPF,
        'senderAreaCode'            => 83,
        'senderPhone'               => $senderPhone,
        'senderEmail'               => $senderEmail,
        'shippingAddressStreet'     => $shippingAddressStreet,
        'shippingAddressNumber'     => $shippingAddressNumber,
        'shippingAddressDistrict'   => $shippingAddressDistrict,
        'shippingAddressPostalCode' => $shippingAddressPostalCode,
        'shippingAddressCity'       => $shippingAddressCity,
        'shippingAddressState'      => $shippingAddressState,
        'shippingAddressCountry'    => 'BRA',
        'shippingType'              => 1,
        'shippingCost'              => '1.00',
        'installmentQuantity'       => 1,
        'installmentValue'          => '3.00',
        'creditCardHolderName'      => 'Chuck Norris',
        'creditCardHolderCPF'       => '54793120652',
        'creditCardHolderBirthDate' => '01/01/1990',
        'creditCardHolderAreaCode'  => 83,
        'creditCardHolderPhone'     => '999999999',
        'billingAddressStreet'     => 'Address',
        'billingAddressNumber'     => '1234',
        'billingAddressDistrict'   => 'Bairro',
        'billingAddressPostalCode' => '58075000',
        'billingAddressCity'       => 'João Pessoa',
        'billingAddressState'      => 'PB',
        'billingAddressCountry'    => 'BRA'
    );

Note that you need to repeat according to the number of products that part

        'itemId1'                   => '0001', 
        'itemDescription1'          => 'PHP Test',  
        //'itemAmount1'               => $numero,  
        'itemAmount1'               => '1.00',  
        'itemQuantity1'             => 1,

        'itemId2'                   => '0002',
        'itemDescription2'          => 'PHP Test2',  
        //'itemAmount2'               => $numero,  
        'itemAmount2'               => '1.00',  
        'itemQuantity2'             => 1,   

Products come from an array

foreach($product_list_array as $item)
{
    echo $item->product_price;
    echo $item->product_id;
    echo $item->product_quantity;
    echo $item->product_desc;

}

How would I put this foreach into the $ params = array (...) along with the other purchase information?

Would it look like this?

<?php 

$params = array();

$params['email']  = $PAGSEGURO_EMAIL;  
$params['token']  = $PAGSEGURO_TOKEN;

foreach($product_list_array as $index => $item)
{
    $item = 'itemId' . $index;
    $params[$item]  => $item->product_id,
    $params['itemDescription1'] = $item->product_desc,  
    $params['itemAmount1'] = $item->product_price,  
    $params['itemQuantity1'] = $item->product_quantity,
}

$params['billingAddressState'] => 'PB';
$params['billingAddressCountry'] => 'BRA';

$header = array('Content-Type' => 'application/json; charset=UTF-8;');
    $response = curlExec($PAGSEGURO_API_URL."/transactions", $params, $header);
    $json = json_decode(json_encode(simplexml_load_string($response)));

?>

is giving error

    
asked by anonymous 04.09.2017 / 18:54

1 answer

1
$params['email']  = $PAGSEGURO_EMAIL;  
$params['token']  = $PAGSEGURO_TOKEN;
...

foreach($product_list_array as $index => $item)
{
    $item = 'itemId' . $index;
    $params[$item]  = $item->product_id;
    $params['itemDescription1'] = $item->product_desc; 
    $params['itemAmount1'] = $item->product_price;
    $params['itemQuantity1'] = $item->product_quantity;
}

...
$params['billingAddressState'] = 'PB';
$params['billingAddressCountry'] = 'BRA';
    
04.09.2017 / 19:05