mallen Posted March 12, 2013 Share Posted March 12, 2013 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,'.',''); } Quote Link to comment https://forums.phpfreaks.com/topic/275562-rewriting-an-array/ Share on other sites More sharing options...
Barand Posted March 12, 2013 Share Posted March 12, 2013 do you want $i = 1; foreach($Order->Cart->contents as $Item) { $lineItems[] = array ( 'itemId' => $i++, 'name' => $Item->name, 'quantity' => $Item->quantity, 'unitPrice' => $Item->unitprice ); } Quote Link to comment https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418240 Share on other sites More sharing options...
mallen Posted March 12, 2013 Author Share Posted March 12, 2013 (edited) 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]]>' )); Edited March 12, 2013 by mallen Quote Link to comment https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418244 Share on other sites More sharing options...
Barand Posted March 12, 2013 Share Posted March 12, 2013 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); Quote Link to comment https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418249 Share on other sites More sharing options...
mallen Posted March 13, 2013 Author Share Posted March 13, 2013 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]]>' )); Quote Link to comment https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418362 Share on other sites More sharing options...
Barand Posted March 13, 2013 Share Posted March 13, 2013 No. $whatYouWant should contain what you want. (I believe in meaningful variable names). Echo it and check Quote Link to comment https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418366 Share on other sites More sharing options...
mallen Posted March 13, 2013 Author Share Posted March 13, 2013 $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? Quote Link to comment https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418371 Share on other sites More sharing options...
mallen Posted March 13, 2013 Author Share Posted March 13, 2013 (edited) 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. Edited March 13, 2013 by mallen Quote Link to comment https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418383 Share on other sites More sharing options...
mallen Posted March 13, 2013 Author Share Posted March 13, 2013 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418385 Share on other sites More sharing options...
mallen Posted March 13, 2013 Author Share Posted March 13, 2013 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418386 Share on other sites More sharing options...
mallen Posted March 13, 2013 Author Share Posted March 13, 2013 Its converting the > symbol to => I tried htmlspecialchars($whatIWant,ENT_COMPAT, 'UTF-8') and htmlspecialchars($whatIWant, ENT_QUOTES, 'UTF-8') Quote Link to comment https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418407 Share on other sites More sharing options...
Zane Posted March 13, 2013 Share Posted March 13, 2013 (edited) 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); Edited March 13, 2013 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418416 Share on other sites More sharing options...
mallen Posted March 13, 2013 Author Share Posted March 13, 2013 (edited) 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> Edited March 13, 2013 by mallen Quote Link to comment https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418419 Share on other sites More sharing options...
Barand Posted March 13, 2013 Share Posted March 13, 2013 Are you sure it wants the array as you specified or does it really want nested XML elements in there? Quote Link to comment https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418422 Share on other sites More sharing options...
mallen Posted March 13, 2013 Author Share Posted March 13, 2013 (edited) 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', ), Edited March 13, 2013 by mallen Quote Link to comment https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418430 Share on other sites More sharing options...
mallen Posted March 13, 2013 Author Share Posted March 13, 2013 I have tried this also. $whatIWant = '\'lineItems\' = ' . htmlentities(var_export($lineItems, 1)) ; Quote Link to comment https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418439 Share on other sites More sharing options...
mallen Posted March 13, 2013 Author Share Posted March 13, 2013 I have tried $whatIWant = '\'lineItems\' = ' . html_entity_decode(var_export($lineItems, 1)) ; Quote Link to comment https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418454 Share on other sites More sharing options...
mallen Posted March 13, 2013 Author Share Posted March 13, 2013 $whatIWant = '\'lineItems\' = ' . htmlspecialchars_decode(var_export($lineItems, 1),ENT_QUOTES) ; Tried this Quote Link to comment https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418478 Share on other sites More sharing options...
mallen Posted March 15, 2013 Author Share Posted March 15, 2013 Any other ideas I could try? Quote Link to comment https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1418790 Share on other sites More sharing options...
mallen Posted March 18, 2013 Author Share Posted March 18, 2013 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> Quote Link to comment https://forums.phpfreaks.com/topic/275562-rewriting-an-array/#findComment-1419320 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.