Jump to content

Rewriting an array


mallen

Recommended Posts

I am working with a API and need to change the format of the data I send. I need this

 

// Line Items
$i = 1;
foreach($Order->Cart->contents as $Item) {
$_['x_line_item'][] = ($i++)."<|>".substr($Item->name,0,31)."<|>".((sizeof($Item->options) > 1)?"     (".substr($Item->option->label,0,253).")":"")."<|>".(int)$Item->quantity."<|>".number_format($Item->unitprice,$this->precision,'.','')."<|>".(($Item->tax)?"Y":"N");
		}

 Converted to this:

                 'lineItems' => array(
                    0 => array(
                        'itemId' => '1',
                        'name' => $Item->name,
                        'quantity' => $Item->quantity,
                        'unitPrice' => $Item->unitprice
                    ),
                    1 => array(
                        'itemId' => '2',
                        'name' => $Item->name,
                        'quantity' => $Item->quantity,
                        'unitPrice' => $Item->unitprice
                    )

I tried it like this but I don't have it correct:

                $i = 1;
		foreach($Order->Cart->contents as $Item) {
			'lineItems' = ($i++).

			"<|>".substr($Item->name,0,31).
			
			"<|>".(int)$Item->quantity.
			"<|>".number_format($Item->unitPrice,$this->precision,'.','');
			}

 

Link to comment
https://forums.phpfreaks.com/topic/275562-rewriting-an-array/
Share on other sites

It needs to look like this when its sent.

 

 $xml = new AuthnetXML(AUTHNET_LOGIN, AUTHNET_TRANSKEY, AuthnetXML::USE_DEVELOPMENT_SERVER);
    $xml->createCustomerProfileTransactionRequest(array(
        'transaction' => array(
            'profileTransAuthCapture' => array(
                'amount' => '10.95',
                'tax' => array(
                    'amount' => '1.00',
                    'name' => 'state sales tax',
                    'description' => 'your state sales tax'
                ),
                'shipping' => array(
                    'amount' => '2.00',
                    'name' => 'ground based shipping',
                    'description' => 'Ground based 5 to 10 day shipping'
                ),
                'lineItems' => array(  ////////this is the part I need from here
                    0 => array(
                        'itemId' => '1',
                        'name' => 'widget',
                        'quantity' => '5',
                        'unitPrice' => '10.00'
                    ),
                    1 => array(
                        'itemId' => '2',
                        'name' => 'another widget',
                        'quantity' => '20',
                        'unitPrice' => '10.00'
                    )
                ), /////////////////to here
                'customerProfileId' => '345',
                'customerPaymentProfileId' => '675',
                'customerShippingAddressId' => '453',
                'order' => array(
                    'invoiceNumber' => 'INV000001',
                    'description' => 'description of transaction',
                    'purchaseOrderNumber' => '000001'
                ),
                'taxExempt' => 'false',
                'recurringBilling' => 'false',
                'cardCode' => '000'
            )
        ),
        'extraOptions' => '<![CDATA[x_customer_ip=100.0.0.1]]>'
    ));
Link to comment
https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418244
Share on other sites

perhaps this

 

$i = 1;
foreach($Order->Cart->contents as $Item) {
    $lineItems[] = array (
        'itemId' => $i++,
        'name' => $Item->name,
        'quantity' => $Item->quantity,
        'unitPrice' => $Item->unitprice
    );
}

$whatYouWant = 'lineItems = ' . var_export($lineItems, 1);
Link to comment
https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418249
Share on other sites

Like this? I need the commas and closing brackets in the correct place.

 $xml = new AuthnetXML(AUTHNET_LOGIN, AUTHNET_TRANSKEY, AuthnetXML::USE_DEVELOPMENT_SERVER);
    $xml->createCustomerProfileTransactionRequest(array(
        'transaction' => array(
            'profileTransAuthCapture' => array(
                'amount' => '10.95',
                'tax' => array(
                    'amount' => '1.00',
                    'name' => 'state sales tax',
                    'description' => 'your state sales tax'
                ),
                'shipping' => array(
                    'amount' => '2.00',
                    'name' => 'ground based shipping',
                    'description' => 'Ground based 5 to 10 day shipping'
                ),
                 
                $i = 1;
		foreach($Order->Cart->contents as $Item) {
    		$lineItems[] = array (
       		'itemId' => $i++,
        	'name' => $Item->name,
        	'quantity' => $Item->quantity,
        	'unitPrice' => $Item->unitprice
    );
}  
		$whatYouWant = 'lineItems = ' . var_export($lineItems, 1);
                'customerProfileId' => '345',
                'customerPaymentProfileId' => '675',
                'customerShippingAddressId' => '453',
                'order' => array(
                    'invoiceNumber' => 'INV000001',
                    'description' => 'description of transaction',
                    'purchaseOrderNumber' => '000001'
                ),
                'taxExempt' => 'false',
                'recurringBilling' => 'false',
                'cardCode' => '000'
            )
        ),
        'extraOptions' => '<![CDATA[x_customer_ip=100.0.0.1]]>'
    ));


Link to comment
https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418362
Share on other sites

  $i = 1;
		foreach($Order->Cart->contents as $Item) {
    		$lineItems[] = array (
       		'itemId' => $i++,
        	'name' => $Item->name,
        	'quantity' => $Item->quantity,
        	'unitPrice' => $Item->unitprice
    );
} 
 $whatIWant = 'lineItems = ' . var_export($lineItems, 1)
 
 $xml = new AuthnetXML(AUTHNET_LOGIN, AUTHNET_TRANSKEY, AuthnetXML::USE_DEVELOPMENT_SERVER);
    $xml->createCustomerProfileTransactionRequest(array(
        'transaction' => array(
            'profileTransAuthCapture' => array(
                'amount' => '10.95',
                'tax' => array(
                    'amount' => '1.00',
                    'name' => 'state sales tax',
                    'description' => 'your state sales tax'
                ),
                'shipping' => array(
                    'amount' => '2.00',
                    'name' => 'ground based shipping',
                    'description' => 'Ground based 5 to 10 day shipping'
                ),
                 
            	echo $whatIWant;
                 ),

                'customerProfileId' => '345',
                'customerPaymentProfileId' => '675',
                'customerShippingAddressId' => '453',
                'order' => array(
                    'invoiceNumber' => 'INV000001',
                    'description' => 'description of transaction',
                    'purchaseOrderNumber' => '000001'
                ),
                'taxExempt' => 'false',
                'recurringBilling' => 'false',
                'cardCode' => '000'
            )
        ),
        'extraOptions' => '<![CDATA[x_customer_ip=100.0.0.1]]>'
    ));


Like this?

Link to comment
https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418371
Share on other sites

I edited this line adding  \   ran and echoed the results outside of the main script and it looked correct. So that's a start.

 $whatIWant = '\'lineItems\' = ' . var_export($lineItems, 1) ;

I am getting an error when I process the script saying text is not allowed. It seems to not like the echo$whatIWant inthe middle of the script.

Link to comment
https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418383
Share on other sites


<?xml version="1.0"?>
<createCustomerProfileTransactionRequest>
<merchantAuthentication>
<name>xxx</name>
<transactionKey>xxx</transactionKey>
</merchantAuthentication>
<transaction>'lineItems' = array (
0 =>
array (
'itemId' => 1,
'name' => 'Some Widget',
'quantity' => '1',
'unitPrice' => 1,
),
)</transaction>


The error is : The element 'transaction' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd' cannot contain text.

 

Link to comment
https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418385
Share on other sites


<?xml version="1.0"?>
<createCustomerProfileTransactionRequest>
<merchantAuthentication>
<name>xxx</name>
<transactionKey>xxx</transactionKey>
</merchantAuthentication>
<transaction>'lineItems' = array (
0 =>
array (
'itemId' => 1,
'name' => 'Some Widget',
'quantity' => '1',
'unitPrice' => 1,
),
)</transaction>


The error is : The element 'transaction' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd' cannot contain text.

 

Link to comment
https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418386
Share on other sites

I would do as Barrand suggested yet add the lineItems key to it.

 

 

 

$i = 1;
foreach($Order->Cart->contents as $Item) {
    $lineItems['lineItems'][] = array (
   'itemId' => $i++,
   'name' => $Item->name,
   'quantity' => $Item->quantity,
   'unitPrice' => $Item->unitprice
   );
}

$whatYouWant = var_export($lineItems, 1);
 
Link to comment
https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418416
Share on other sites

Sorry maybe my previous post was not clear. Its returning the XML and showing that line items. Its just the formatting of the > that I need fixed.

 

<?xml version="1.0"?>
<createCustomerProfileTransactionRequest>
  <merchantAuthentication>
    <name>xxx</name>
    <transactionKey>xxx</transactionKey>
  </merchantAuthentication>
  <transaction>'lineItems' = array (
  0 => 
  array (
    'itemId' => 1,
    'name' => 'Some Widget',
    'quantity' => '1',
    'unitPrice' => 1,
  ),
)</transaction>

Should be returned as:

<?xml version="1.0"?>
<createCustomerProfileTransactionRequest>
  <merchantAuthentication>
    <name>xxx</name>
    <transactionKey>xxx</transactionKey>
  </merchantAuthentication>
  <transaction>'lineItems' = array (
  0 => array (
    'itemId' => 1,
    'name' => 'Some Widget',
    'quantity' => '1',
    'unitPrice' => 1,
  ),
)</transaction>
Link to comment
https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418419
Share on other sites

Yes it wants it just like this. Sorry had a couple typos. See post #3 for what it should look like for the items. I have used a hard coded version before and have successfully submitted the XML.

 <transaction>'lineItems' = array (
  0 => array (
    'itemId' => '1',
    'name' => 'Some Widget',
    'quantity' => '1',
    'unitPrice' => '1',
  ),

Link to comment
https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418430
Share on other sites

I tried something like this

 

    $i = 1;
    foreach($Order->Cart->contents as $Item) {
        $lineItems[] = array (
            'lineItem' => array(
                'itemId' => $i++,
                'name' => $Item->name,
                'quantity' => $Item->quantity,
                'unitPrice' => number_format($Item->unitprice,$this->precision,'.','')//format to 1.00
             )
        );
    }
     
    $xml->createCustomerProfileTransactionRequest(array(
            'transaction' => array(
                'profileTransAuthCapture' => array(
                    'amount' => $Order->Cart->Totals->total
               ),
           'lineItems' => $lineItems, //changed this line
           'customerProfileId' => '135',
           'customerPaymentProfileId' => '186',
           'customerShippingAddressId' => '120',
           'order' => array(
               'invoiceNumber' => 'IN8',
               'description' => 'description of transaction',
               'purchaseOrderNumber' => 'PO4'
           ),
           'taxExempt' => 'false',
           'recurringBilling' => 'false',
           'cardCode' => '000'
       )
    ); 

But it outputs this. There should only be one opening <lineItems>  and one closing </lineItems>  I am getting cose.

 

<lineItems>
        <lineItem>
          <itemId>1</itemId>
          <name>widget name</name>
          <quantity>3</quantity>
          <unitPrice>5.00</unitPrice>
        </lineItem>
      </lineItems>
      <lineItems>
        <lineItem>
          <itemId>2</itemId>
          <name>widget 2 name</name>
          <quantity>4</quantity>
          <unitPrice>6.00</unitPrice>
        </lineItem>
      </lineItems>  
Link to comment
https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1419320
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.