Foreach inside another array in php

Asked

Viewed 1,426 times

0

How can I generate with php this array to make a payment transaction by 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,   

Product comes 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 within the $params=array(...) along with other purchase information?

Would 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 making a mistake

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';
  • so it gave error when I put inside the $params();

  • try to initialize $params = array();

  • look how I did, edited my answer. is giving error

  • In the example I sent you I put [...] you completed with the other parameters I jumped? you can check that all items were correctly placed under $params['billingAddressCountry'] => 'BRA'; the command: var_export($params); die();, so before you run the application you press F12 goes into the network tab and runs the application, so the application will stop in the "die();" and in the network tab you will have the values of $params.

  • Yes I put the other parameters, my code is according to my answer where it says "Stayed like this" is giving php error same.

  • What’s the mistake? You can post?

  • yes, it looks like this https://imgur.com/a/EOe2N

  • The images are not of the error, and according to the code you sent it is not with the other parameters

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.