I am attempting to parse CSV strings into a multi-dimensional array. I am using the following code to do this...
public function exportPartsAuthority($fileArray)
{
foreach ($fileArray as $filename => $fileContent) {
$lines = explode("\n", $fileContent);
$formatting = explode(",", $lines[0]);
unset($lines[0]);
$results = array();
foreach ( $lines as $line ) {
$parsedLine = str_getcsv( $line, ',' );
$result = array();
foreach ( $formatting as $index => $caption ) {
if(isset($parsedLine[$index])) {
$result[$formatting[$index]] = trim($parsedLine[$index]);
} else {
$result[$formatting[$index]] = '';
}
}
$results[] = $result;
}
$var_str = var_export($results, true);
$var = "<?php\n\n\$$values = $var_str;\n\n?>";
file_put_contents('/home/apndev/public_html/output.txt', $var);
}
}
The end result is this:
$ = array (
0 =>
array (
'Ordernumber' => '100000002',
'Orderdate' => '02/10/2013',
'OrderStatus' => 'pending',
'PurchasedWebsite' => 'Main Website - Main Website Store - Default Store View',
'PaymentMethod' => 'checkmo',
'ShippingMethod' => 'flatrate_flatrate',
'Subtotal' => '2.0000',
'ShippingCost' => '10.0000',
'GrandTotal' => '12.0000',
'TotalTax' => '0.0000',
'TotalPaid' => '',
'TotalRefunded' => '',
'ItemName' => 'K&N Air Filter Wrap',
'ItemSKU' => 'YA-6504PK K&N',
'ItemISBN' => '',
'ItemStock' => '',
'ItemPrice' => '1.0000',
'CostPrice' => '',
'ItemOrdered' => '1.0000',
'ItemInvoiced' => '0.0000',
'ItemSent' => '0.0000',
'CustomerID' => '1',
'BillingFirstName' => 'Brian',
'BillingLastName' => '',
'BillingCompany' => '',
'BillingE-Mail' => '',
'BillingPhone' => '',
'BillingAddress1' => '',
'BillingAddress2' => '',
'BillingCity' => '',
'BillingPostcode' => '',
'BillingState' => 'Florida',
'BillingCountry' => 'US',
'ShippingFirstName' => 'Brian',
'ShippingLastName' => '',
'ShippingCompany' => '',
'ShippingE-Mail' => '',
'ShippingPhone' => '',
'ShippingAddress1' => '',
'ShippingAddress2' => '',
'ShippingCity' => '',
'ShippingPostcode' => '',
'ShippingState' => 'Florida',
'ShippingCountry' => 'US',
'Vendor SKU' => '',
'Line Code
' => '',
),
1 =>
array (
'Ordernumber' => '100000002',
'Orderdate' => '02/10/2013',
'OrderStatus' => 'pending',
'PurchasedWebsite' => 'Main Website - Main Website Store - Default Store View',
'PaymentMethod' => 'checkmo',
'ShippingMethod' => 'flatrate_flatrate',
'Subtotal' => '2.0000',
'ShippingCost' => '10.0000',
'GrandTotal' => '12.0000',
'TotalTax' => '0.0000',
'TotalPaid' => '',
'TotalRefunded' => '',
'ItemName' => 'K&N Air Filter Wrap',
'ItemSKU' => 'YA-6601-TDK K&N',
'ItemISBN' => '',
'ItemStock' => '',
'ItemPrice' => '1.0000',
'CostPrice' => '',
'ItemOrdered' => '1.0000',
'ItemInvoiced' => '0.0000',
'ItemSent' => '0.0000',
'CustomerID' => '1',
'BillingFirstName' => 'Brian',
'BillingLastName' => '',
'BillingCompany' => '',
'BillingE-Mail' => '',
'BillingPhone' => '',
'BillingAddress1' => '',
'BillingAddress2' => '',
'BillingCity' => '',
'BillingPostcode' => '',
'BillingState' => 'Florida',
'BillingCountry' => 'US',
'ShippingFirstName' => 'Brian',
'ShippingLastName' => '',
'ShippingCompany' => '',
'ShippingE-Mail' => '',
'ShippingPhone' => '',
'ShippingAddress1' => '',
'ShippingAddress2' => '',
'ShippingCity' => '',
'ShippingPostcode' => '',
'ShippingState' => 'Florida',
'ShippingCountry' => 'US',
'Vendor SKU' => '',
'Line Code
' => '',
),
2 =>
array (
'Ordernumber' => '100000003',
'Orderdate' => '07/10/2013',
'OrderStatus' => 'pending',
'PurchasedWebsite' => 'Main Website - Main Website Store - Default Store View',
'PaymentMethod' => 'checkmo',
'ShippingMethod' => 'flatrate_flatrate',
'Subtotal' => '1716.5000',
'ShippingCost' => '5.0000',
'GrandTotal' => '1721.5000',
'TotalTax' => '0.0000',
'TotalPaid' => '',
'TotalRefunded' => '',
'ItemName' => 'Cardone High Pressure Diesel Injection Oil Pump',
'ItemSKU' => '2P-225 Cardone',
'ItemISBN' => '',
'ItemStock' => '',
'ItemPrice' => '1716.5019',
'CostPrice' => '',
'ItemOrdered' => '1.0000',
'ItemInvoiced' => '0.0000',
'ItemSent' => '0.0000',
'CustomerID' => '1',
'BillingFirstName' => 'Brian',
'BillingLastName' => '',
'BillingCompany' => '',
'BillingE-Mail' => '',
'BillingPhone' => '',
'BillingAddress1' => '',
'BillingAddress2' => '',
'BillingCity' => '',
'BillingPostcode' => '',
'BillingState' => 'Florida',
'BillingCountry' => 'US',
'ShippingFirstName' => 'Brian',
'ShippingLastName' => '',
'ShippingCompany' => '',
'ShippingE-Mail' => '',
'ShippingPhone' => '',
'ShippingAddress1' => '',
'ShippingAddress2' => '',
'ShippingCity' => '',
'ShippingPostcode' => '',
'ShippingState' => 'Florida',
'ShippingCountry' => 'US',
'Vendor SKU' => '2P-225',
'Line Code
' => 'A1',
),
3 =>
array (
'Ordernumber' => '',
'Orderdate' => '',
'OrderStatus' => '',
'PurchasedWebsite' => '',
'PaymentMethod' => '',
'ShippingMethod' => '',
'Subtotal' => '',
'ShippingCost' => '',
'GrandTotal' => '',
'TotalTax' => '',
'TotalPaid' => '',
'TotalRefunded' => '',
'ItemName' => '',
'ItemSKU' => '',
'ItemISBN' => '',
'ItemStock' => '',
'ItemPrice' => '',
'CostPrice' => '',
'ItemOrdered' => '',
'ItemInvoiced' => '',
'ItemSent' => '',
'CustomerID' => '',
'BillingFirstName' => '',
'BillingLastName' => '',
'BillingCompany' => '',
'BillingE-Mail' => '',
'BillingPhone' => '',
'BillingAddress1' => '',
'BillingAddress2' => '',
'BillingCity' => '',
'BillingPostcode' => '',
'BillingState' => '',
'BillingCountry' => '',
'ShippingFirstName' => '',
'ShippingLastName' => '',
'ShippingCompany' => '',
'ShippingE-Mail' => '',
'ShippingPhone' => '',
'ShippingAddress1' => '',
'ShippingAddress2' => '',
'ShippingCity' => '',
'ShippingPostcode' => '',
'ShippingState' => '',
'ShippingCountry' => '',
'Vendor SKU' => '',
'Line Code
' => '',
),
);
When I attempt to echo $results['Vendor SKU'], or var_dump it.. the result is NULL even though I see a value set above. I also notice there is a syntax error at the top ('$ = array').
That being said, I'm ultimately just trying to get the values from the CSV because some of these values will be used to send order information to a third party vendor.
Can anyone point me in the right direction of how to fix my output so that I'll be able to echo the values correctly?
Thanks!
use the built in php function to parse your csv file rather than re-invent the wheel.
str_getcsv
http://php.net/manual/en/function.str-getcsv.php
There's a good example on the man page that essentially does what you want:
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
{
if(!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
print_r($data);
Simple, use the preset CSV reading method:
$fp = fopen('data.csv', 'r');
$rows = array();
while (($row = fgetcsv($fp)) !== false)
$rows[] = $row;
fclose($fp);
$rows will then contain all data of data.csv as a two-dimensional array.
See fgetcsv() for more info.
Related
I am using array-to-xml package and I have sample (static) data like this one
<RECIEVEITEM operation="Add" REQUESTID="1">
<TRANSACTIONID>264276</TRANSACTIONID>
<ITEMLINE operation="Add">
<KeyID>1</KeyID>
<ITEMNO>7GE65B</ITEMNO>
<QUANTITY>5</QUANTITY>
<ITEMUNIT>UNT</ITEMUNIT>
<UNITRATIO>1</UNITRATIO>
<ITEMRESERVED1 />
<ITEMRESERVED2 />
<ITEMRESERVED3 />
<ITEMRESERVED4 />
<ITEMRESERVED5 />
<ITEMRESERVED6 />
<ITEMRESERVED7 />
<ITEMRESERVED8 />
<ITEMRESERVED9 />
<ITEMRESERVED10 />
<ITEMOVDESC>PRINTER HP DESKJET 2135 (7GE65B) NEW</ITEMOVDESC>
<UNITPRICE />
<ITEMDISCPC />
<TAXCODES />
<GROUPSEQ />
<POSEQ />
<BRUTOUNITPRICE>0</BRUTOUNITPRICE>
<WAREHOUSEID>345346</WAREHOUSEID>
<QTYCONTROL>0</QTYCONTROL>
<RISEQ />
<SNHISTORY operation="Ret"><SERIALNUMBER>A1</SERIALNUMBER><EXPIREDDATE>2020-06-30</EXPIREDDATE><QUANTITY>1</QUANTITY><SNSIGN>1</SNSIGN></SNHISTORY>
<SNHISTORY operation="Ret"><SERIALNUMBER>A2</SERIALNUMBER><EXPIREDDATE>2020-06-30</EXPIREDDATE><QUANTITY>1</QUANTITY><SNSIGN>1</SNSIGN></SNHISTORY>
<SNHISTORY operation="Ret"><SERIALNUMBER>A3</SERIALNUMBER><EXPIREDDATE>2020-06-30</EXPIREDDATE><QUANTITY>1</QUANTITY><SNSIGN>1</SNSIGN></SNHISTORY>
<SNHISTORY operation="Ret"><SERIALNUMBER>A4</SERIALNUMBER><EXPIREDDATE>2020-06-30</EXPIREDDATE><QUANTITY>1</QUANTITY><SNSIGN>1</SNSIGN></SNHISTORY>
<SNHISTORY operation="Ret"><SERIALNUMBER>A5</SERIALNUMBER><EXPIREDDATE>2020-06-30</EXPIREDDATE><QUANTITY>1</QUANTITY><SNSIGN>1</SNSIGN></SNHISTORY>
<RIID />
</ITEMLINE>
<INVOICENO>PO-TEST1</INVOICENO>
<INVOICEDATE>2020-06-30</INVOICEDATE>
<TAX1ID>T</TAX1ID>
<TAX1CODE>T</TAX1CODE>
<TAX2CODE />
<TAX1RATE>10</TAX1RATE>
<TAX2RATE>0</TAX2RATE>
<RATE>1</RATE>
<INCLUSIVETAX>0</INCLUSIVETAX>
<INVOICEISTAXABLE>1</INVOICEISTAXABLE>
<CASHDISCOUNT>0</CASHDISCOUNT>
<CASHDISCPC />
<INVOICEAMOUNT>0</INVOICEAMOUNT>
<TERMSID>Net 30</TERMSID>
<FOB />
<PURCHASEORDERNO />
<WAREHOUSEID>345346</WAREHOUSEID>
<DESCRIPTION>PO-TEST1</DESCRIPTION>
<SHIPDATE>2020-06-30</SHIPDATE>
<POSTED>0</POSTED>
<FISCALRATE>1</FISCALRATE>
<INVFROMPR />
<TAXDATE>2020-06-30</TAXDATE>
<VENDORID>PT ECS INDO JAYA</VENDORID>
<SEQUENCENO>PO-TEST1</SEQUENCENO>
<APACCOUNT>2000.01</APACCOUNT>
<SHIPVENDID />
<INVTAXNO2 />
<INVTAXNO1 />
<SSPDATE />
<EXPENSESOFBILLID />
<EXPENSESJOURNALDATETYPE />
<LOCKED_BY />
<LOCKED_TIME />
</RECIEVEITEM>
As you can see tag SNHISTORY includes array, since RECIEVEITEM itself is inside an array I would like to know how can I achieve to have SNHISTORY array?
Code
Commented issue part for you to find is faster :)
public function exportReturn(Request $request) {
$returnProducts = ReturnProduct::with(['barcode', 'barcode.product', 'barcode.product.allBarcodes', 'outlet', 'user',])->get();
foreach($returnProducts as $item) {
$year = Carbon::createFromFormat('Y-m-d H:i:s', $item['created_at'])->year;
$month = Carbon::createFromFormat('Y-m-d H:i:s', $item['created_at'])->month;
$array = [
"TRANSACTIONS" => [
'_attributes' => [
'OnError' => 'CONTINUE'
],
'RECIEVEITEM' => [ // image 1
'_attributes' => [
'operation' => 'Add',
'REQUESTID' => '1'
],
'TRANSACTIONID' => $item['id'],
'ITEMLINE' => [
'_attributes' => [
'operation' => 'Add'
],
'KeyID' => $item['barcode']['product']['id'],
'ITEMNO' => $item['barcode']['product']['sku'],
'QUANTITY' => $item['barcodes']['product']->allBarcodes->count(),
'ITEMUNIT' => $item['barcode']['product']['unit'],
'UNITRATIO' => '1',
'ITEMRESERVED1' => '',
'ITEMRESERVED2' => '',
'ITEMRESERVED3' => '',
'ITEMRESERVED4' => '',
'ITEMRESERVED5' => '',
'ITEMRESERVED6' => '',
'ITEMRESERVED7' => '',
'ITEMRESERVED8' => '',
'ITEMRESERVED9' => '',
'ITEMRESERVED10' => '',
'ITEMOVDESC' => $item['barcode']['product']['name'],
'UNITPRICE' => $item['barcode']['product']['price'],
'ITEMDISCPC' => '',
'TAXCODES' => '',
'GROUPSEQ' => '',
'POSEQ' => '',
'BRUTOUNITPRICE' => '0',
'WAREHOUSEID' => $item['outlet']['code'],
'QTYCONTROL' => '0',
'RISEQ' => '',
'SNHISTORY' => [ // array of product barcodes
'_attributes' => [
'operation' => 'Ret'
],
foreach($item['barcodes']['product']->allBarcodes as $bb) { // this is WRONG (is just here for you to know about data that I need to place here "logic purpose only")
'SERIALNUMBER' => $bb['serial_number'] ? $bb['serial_number'] : $bb['u_serial_number'],
'EXPIREDDATE' => $bb['created_at'],
'QUANTITY' => '1',
'SNSIGN' => '1',
}
],
'RIID' => '',
],
'INVOICENO' => 'POINS-001', //test
'INVOICEDATE' => '2020-06-10', //test
'TAX1ID' => 'T', //test
'TAX1CODE' => 'T', //test
'TAX2CODE' => '',
'TAX1RATE' => '10', //test
'TAX2RATE' => '0', //test
'RATE' => '1', //test
'INCLUSIVETAX' => '0', //test
'INVOICEISTAXABLE' => '1', //test
'CASHDISCOUNT' => '0', //test
'CASHDISCPC' => '',
'INVOICEAMOUNT' => '0', //test
'TERMSID' => 'Net 30', //test
'FOB' => '',
'PURCHASEORDERNO' => '',
'WAREHOUSEID' => $outlet['code'], //test
'DESCRIPTION' => '', // reason of return goes here
'SHIPDATE' => '2020-06-10', //test
'POSTED' => '0', //test
'FISCALRATE' => '1', //test
'INVFROMPR' => '',
'TAXDATE' => '2020-06-10', //test
'VENDORID' => 'PT ECS INDO JAYA', //test
'SEQUENCENO' => 'POINS-001', //test
'APACCOUNT' => '2000.01', //test
'SHIPVENDID' => '',
'INVTAXNO2' => '',
'INVTAXNO1' => '',
'SSPDATE' => '',
'EXPENSESOFBILLID' => '',
'EXPENSESJOURNALDATETYPE' => '',
'LOCKED_BY' => '',
'LOCKED_TIME' => '',
]
]
];
}
$filename = 'returns.xml';
$result = ArrayToXml::convert($array, [
'rootElementName' => 'NMEXML',
'_attributes' => [
'EximID'=> "12551",
'BranchCode'=> $item['outlet']['code'],
'ACCOUNTANTCOPYID'=> ""
]
]);
Storage::disk('local')->put($filename, $result);
$fullPath = url('exports', $filename);
return response()->json([
'data' => $fullPath
]);
}
Questions
How to loop SNHISTORY data?
UPDATE
I have changed my code like:
$array = [];
foreach($returnProducts as $item) {
$year = Carbon::createFromFormat('Y-m-d H:i:s', $item['created_at'])->year;
$month = Carbon::createFromFormat('Y-m-d H:i:s', $item['created_at'])->month;
foreach($item['barcode']['product']->allBarcodes as $bb) {
$shin[] = [
'SERIALNUMBER' => $bb['serial_number'] ? $bb['serial_number'] : $bb['u_serial_number'],
'EXPIREDDATE' => $bb['created_at'],
'QUANTITY' => '1',
'SNSIGN' => '1',
];
}
$array['RECIEVEITEM'] = [
'_attributes' => [
'operation' => 'Add',
'REQUESTID' => '1'
],
'TRANSACTIONID' => $item['id'],
'ITEMLINE' => [
'_attributes' => [
'operation' => 'Add'
],
'KeyID' => $item['barcode']['product']['id'],
'ITEMNO' => $item['barcode']['product']['sku'],
'QUANTITY' => $item['barcode']['product']->allBarcodes->count(),
'ITEMUNIT' => $item['barcode']['product']['unit'],
'UNITRATIO' => '1',
'ITEMRESERVED1' => '',
'ITEMRESERVED2' => '',
'ITEMRESERVED3' => '',
'ITEMRESERVED4' => '',
'ITEMRESERVED5' => '',
'ITEMRESERVED6' => '',
'ITEMRESERVED7' => '',
'ITEMRESERVED8' => '',
'ITEMRESERVED9' => '',
'ITEMRESERVED10' => '',
'ITEMOVDESC' => $item['barcode']['product']['name'],
'UNITPRICE' => $item['barcode']['product']['price'],
'ITEMDISCPC' => '',
'TAXCODES' => '',
'GROUPSEQ' => '',
'POSEQ' => '',
'BRUTOUNITPRICE' => '0',
'WAREHOUSEID' => $item['outlet']['code'],
'QTYCONTROL' => '0',
'RISEQ' => '',
'SNHISTORY' => [
'_attributes' => [
'operation' => 'Ret'
],
$shin,
],
'RIID' => '',
],
'INVOICENO' => 'POINS-001', //test
'INVOICEDATE' => '2020-06-10', //test
'TAX1ID' => 'T', //test
'TAX1CODE' => 'T', //test
'TAX2CODE' => '',
'TAX1RATE' => '10', //test
'TAX2RATE' => '0', //test
'RATE' => '1', //test
'INCLUSIVETAX' => '0', //test
'INVOICEISTAXABLE' => '1', //test
'CASHDISCOUNT' => '0', //test
'CASHDISCPC' => '',
'INVOICEAMOUNT' => '0', //test
'TERMSID' => 'Net 30', //test
'FOB' => '',
'PURCHASEORDERNO' => '',
'WAREHOUSEID' => $item['outlet']['code'], //test
'DESCRIPTION' => '', // reason of return goes here
'SHIPDATE' => '2020-06-10', //test
'POSTED' => '0', //test
'FISCALRATE' => '1', //test
'INVFROMPR' => '',
'TAXDATE' => '2020-06-10', //test
'VENDORID' => 'PT ECS INDO JAYA', //test
'SEQUENCENO' => 'POINS-001', //test
'APACCOUNT' => '2000.01', //test
'SHIPVENDID' => '',
'INVTAXNO2' => '',
'INVTAXNO1' => '',
'SSPDATE' => '',
'EXPENSESOFBILLID' => '',
'EXPENSESJOURNALDATETYPE' => '',
'LOCKED_BY' => '',
'LOCKED_TIME' => '',
];
}
Now if I do return response()->json($shin); it returns data just as I need them
But When I try to use $shin in my code (as you see in my update code it doesn't work.
Error? it give error about 100 lines above my code (really not helpful error message)
here is the error in preview tab
exception: "DOMException"
file: "..........\vendor\spatie\array-to-xml\src\ArrayToXml.php"
line: 152
message: "Invalid Character Error"
here is where i track my controller in response tab
{
"file": "........\\app\\Http\\Controllers\\Api\\XmlExportController.php",
"line": 869,
"function": "convert",
"class": "Spatie\\ArrayToXml\\ArrayToXml",
"type": "::"
},
{
"function": "exportReturn",
"class": "App\\Http\\Controllers\\Api\\XmlExportController",
"type": "->"
},
Any idea?
UPDATE 2
Well I managed to get my SNHISTORY looped data as array but the issue is while $item['barcode']['product']->allBarcodes only includes current product barcodes during print somehow it print all barcodes even from other products.
foreach($item['barcode']['product']->allBarcodes as $bb) {
$shin[] = [
'_attributes' => [
'operation' => 'Ret'
],
'SERIALNUMBER' => $bb['serial_number'] ? $bb['serial_number'] : $bb['u_serial_number'],
'EXPIREDDATE' => $bb['created_at'],
'QUANTITY' => '1',
'SNSIGN' => '1',
];
}
and in my xml code
'SNHISTORY' => [
$shin,
],
now it prints like:
result of return response()->json($item['barcode']['product']->allBarcodes);
any idea?
Solved
After doing UPDATE 2 i could fix my extra data by adding $shin = []; above my loop code.
$shin = [];
foreach($item['barcode']['product']->allBarcodes as $bb) {
$shin[] = [
'_attributes' => [
'operation' => 'Ret'
],
'SERIALNUMBER' => $bb->serial_number ? $bb->serial_number : $bb->u_serial_number,
'EXPIREDDATE' => $bb->created_at,
'QUANTITY' => '1',
'SNSIGN' => '1',
];
}
and
'SNHISTORY' => [
$shin,
],
Works as it should be now.
All data in array like this
<?php
$sub_arr = array();
$sub_arr[] = array(
'sr_nm' => 1,
'recept_number' => 3019,
'adm_number' => 3434,
'student_name' => 'amit',
'class' => 'LKG',
'pay' => 'online',
'cheq_nm' => '',
'adm_fee' => '',
'consolidated' => '',
'sec.fee' => '',
'dev.charge' => 5000,
'school_fee' => 1300,
'subling' => '-3400',
'transport' => 2300,
'late_fee' =>'total' => 35006
);
$sub_arr[] = array('sr_nm' => 1,
'recept_number' => 3019,
'adm_number' => 3434,
'student_name' => 'amit',
'class' => 'LKG',
'pay' => 'online',
'cheq_nm' => '',
'adm_fee' => '',
'consolidated' => '',
'sec.fee' => '',
'dev.charge' => 5000,
'school_fee' => 1300,
'subling' => '-3400',
'transport' => 2300,
'late_fee' => '',
'total' => 35006
);
And my excel file:
I want to insert new data from A4 shell after loading the file.
I receive a callback data from payment site and try to save some values into txt file
Getting an array:
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
Saving all data to txt:
file_put_contents('robot.php', var_export($obj, true) . "\r\n\r\n", FILE_APPEND | LOCK_EX);
The result from txt:
array (
'rrn' => '',
'masked_card' => '444455XXXXXX1111',
'sender_cell_phone' => '',
'response_status' => 'success',
'sender_account' => '',
'fee' => '',
'rectoken_lifetime' => '',
'reversal_amount' => '0',
'settlement_amount' => '0',
'actual_amount' => '370000',
'order_status' => 'approved',
'response_description' => '',
'verification_status' => '',
'order_time' => '04.01.2018 21:53:47',
'actual_currency' => 'JPY',
'order_id' => 1515095627',
'parent_order_id' => '',
'merchant_data' => '[{"name":"custom-field-0","label":"fist_name","value":"john_doe"},{"name":"custom-field-2","label":"phone","value":"07777777777"}]',
'tran_type' => 'purchase',
'eci' => '5',
'settlement_date' => '',
'payment_system' => 'card',
'rectoken' => '',
'approval_code' => '56783909',
'merchant_id' => 14066776208,
'settlement_currency' => '',
'payment_id' => 7487818gg3,
'product_id' => '',
'amount' => '370000',
'sender_email' => 'admin#none.in.ua',
)
How to save in txt separate values from nested array 'merchant_data' - first name and phone?
You can make new array and save it to file:
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
$save = [];
foreach ($obj['merchant_data'] as $data){
$save[] = [
'name' => $data['first_name'],
'phone' => $data['phone'],
];
}
file_put_contents('robot.php', var_export($save, true) . "\r\n\r\n", FILE_APPEND | LOCK_EX);
I am creating a simple import script for attributes and I cannot figure out how to add options to them. Every attribute of the 'Attribute' is straight forward, except for adding an option. Is this something that can be done upon the creation of an Attribute??
The code I use basically is below.
$model = Mage::getModel('catalog/resource_eav_attribute');
$data = array(
'is_global' => '0',
'frontend_input' => 'text',
'default_value_text' => '',
'default_value_yesno' => '0',
'default_value_date' => '',
'default_value_textarea' => '',
'is_unique' => '0',
'is_required' => '0',
'frontend_class' => '',
'is_searchable' => '1',
'is_visible_in_advanced_search' => '1',
'is_comparable' => '1',
'is_used_for_promo_rules' => '0',
'is_html_allowed_on_front' => '1',
'is_visible_on_front' => '0',
'used_in_product_listing' => '0',
'used_for_sort_by' => '0',
'is_configurable' => '0',
'is_filterable' => '0',
'is_filterable_in_search' => '0',
'backend_type' => 'varchar',
'default_value' => '',
'frontend_label' => '',
'attribute_code' => ''
);
foreach ($header as $key => $value){
if(isset($data[$key]) !== false){
$data[$key] = $row[$header[$key]];
}
}
$data['option'] = ?WHAT DO I DO HERE¿
$model->addData($data);
$model->setEntityTypeId(Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId());
$model->setIsUserDefined(1);
$model->save();
}
EDIT:
Thanks to Marko for his example, I tried the following;
$data['option'] = array (
'value' => array(
'wood' => array('Wood'),
'metal' => array('Metal')
)
);
His method for adding attributes in general is slightly different but the value for that attribute works just the same.
W00t!
you can create sql script (tutorial: http://alanstorm.com/magento_setup_resources) and inside put something like:
$installer = $this;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$setup->addAttribute('catalog_product', 'attr_code', array(
'group' => 'General',
'input' => 'select',
'type' => 'text',
'label' => 'Material',
'backend' => '',
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'searchable' => 1,
'filterable' => 0,
'comparable' => 1,
'visible_on_front' => 1,
'source' => 'eav/entity_attribute_source_table',
'visible_in_advanced_search' => 0,
'is_html_allowed_on_front' => 0,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'configurable' => 1,
'option' => array (
'value' => array(
'wood' => array('Wood'),
'metal' => array('Metal')
)
),
));
$installer->endSetup();
this should create attribute Material with Wood and Metal options
I'm having an issue unsetting my parent if I do not find my needle in my child, for some reason I just cannot get this to work no matter how many different ways I try it, could someone possibly point me in the right direction? This is being thrown at a multidimensional array with a floor depth of about 4. Here's the code and an example of what a slice of the array might look like. In this case only Array[3] should remain, Array's [1-2] should be removed.
array (
1 =>
array (
197015 =>
array (
345415 =>
array (
'options' =>
array (
'Name on Credential' => '',
'Ordination Month' => '',
'Ordination Day' => '',
'Ordination Year' => '',
'Badge Choice?' => '',
),
'comments' =>
array (
213354 => '',
),
'products_name' => '',
'products_quantity' => '',
'delivery_country' => '',
'customers_name' => '',
'delivery_name' => '',
'delivery_street_address' => '',
'delivery_city' => '',
'delivery_postcode' => '',
'delivery_state' => '',
'customers_telephone' => '',
),
),
),
2 =>
array (
197014 =>
array (
345414 =>
array (
'options' =>
array (
'Name on Credential' => '',
'Ordination Month' => '',
'Ordination Day' => '',
'Ordination Year' => '',
'Badge Choice?' => '',
),
'comments' =>
array (
213353 => '',
),
'products_name' => '',
'products_quantity' => '',
'delivery_country' => '',
'customers_name' => '',
'delivery_name' => '',
'delivery_street_address' => '',
'delivery_city' => '',
'delivery_postcode' => '',
'delivery_state' => '',
'customers_telephone' => '',
),
),
),
3 =>
array (
197013 =>
array (
345412 =>
array (
'options' =>
array (
'Name on Credential' => '',
'Ordination Month' => '',
'Ordination Day' => '',
'Ordination Year' => '',
),
'comments' =>
array (
213352 => '',
),
'products_name' => 'Jedi',
'products_quantity' => '1',
'delivery_country' => '',
'customers_name' => '',
'delivery_name' => '',
'delivery_street_address' => '',
'delivery_city' => '',
'delivery_postcode' => '',
'delivery_state' => '',
'customers_telephone' => '',
),
345413 =>
array (
'options' =>
array (
'' => '',
),
'comments' =>
array (
213352 => '',
),
'products_name' => '',
'products_quantity' => '',
'delivery_country' => '',
'customers_name' => '',
'delivery_name' => '',
'delivery_street_address' => '',
'delivery_city' => '',
'delivery_postcode' => '',
'delivery_state' => '',
'customers_telephone' => '',
),
),
),
Here's a sample of the last code I tried using without success, if you replace unset($k); with return false; it will return which secondary array the needle appears in.
// array_search with recursive searching, optional partial matches and optional search by key
function array_find_r($needle, &$haystack, $partial_matches = false, $search_keys = false) {
if(!is_array($haystack)) return false;
foreach($haystack as $key=>&$value) {
$what = ($search_keys) ? $key : $value;
if($needle===$what) return $key;
else if($partial_matches && #strpos($what, $needle)!==false) return $key;
else if(is_array($value) && array_find_r($needle, $value, $partial_matches, $search_keys)!==false) return $key;
}
unset($k);
}
$tty = array();
foreach($datas as &$k) {
$tty[] = array_find_r('Jedi', &$k, true, false);
}
$tty=array_filter($tty); rsort($tty);
echo '<pre>'; var_export($datas); echo '</pre>';
Why not push the data you do need into a new array and unset the whole old array?