Related
Screenshot
As you see my data are loaded inside extra SNHISTORY array and I need to remove that extra array.
Code
Lines regarding to screenshot results are commented.
$array = [];
foreach($internalTransits as $key => $item) {
foreach($item->barcodes as $barcode){}
$a = $item->barcodes;
$grouped = $a->mapToGroups(function ($item, $key) {
return [
'SNHISTORY' => [ // my arrays to move out
'_attributes' => [
'operation' => 'Ret'
],
'SERIALNUMBER' => $item['serial_number'] ? $item['serial_number'] : $item['u_serial_number'],
'EXPIREDDATE' => $item['created_at']->format('Y-m-d'),
'QUANTITY' => '1',
'SNSIGN' => '-1',
],
'ITEMUNIT' => $item['product']['unit'],
'UNITPRICE' => $item['product']['price'],
];
});
$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;
$timeline[$key][] = [
'_attributes' => [
'operation' => 'Add'
],
'KeyID' => $barcode['product']['id'],
'ITEMNO' => $barcode['product']['sku'],
'QUANTITY' => '1',
'ITEMUNIT' => $barcode['product']['unit'],
'UNITRATIO' => '1',
'ITEMRESERVED1' => '',
'ITEMRESERVED2' => '',
'ITEMRESERVED3' => '',
'ITEMRESERVED4' => '',
'ITEMRESERVED5' => '',
'ITEMRESERVED6' => '',
'ITEMRESERVED7' => '',
'ITEMRESERVED8' => '',
'ITEMRESERVED9' => '',
'ITEMRESERVED10' => '',
'UNITPRICE' => $barcode['product']['price'],
'QTYCONTROL' => '0',
'SNHISTORY' => $grouped->toArray(), // this has extra array where my actual arrays are loaded inside of it
];
$array['TRANSACTIONS'] = [
'_attributes' => [
'OnError' => 'CONTINUE'
],
];
$array['TRANSACTIONS']['WTRAN'] = [
'_attributes' => [
'operation' => 'Add',
'REQUESTID' => '1',
],
'TRANSFERID' => $item['id'],
'TRANSACTIONID' => '',
'TRANSFERNO' => $item['transNu'],
'TRANSFERDATE' => $item['created_at']->format('Y-m-d'),
'DESCRIPTION' => $item['description'],
'FROMWHID' => $barcode['outlet'][0]['name'],
'TOWHID' => $item->toOutlet->name,
'FROMWHADDRESS' => '',
'TOWHADDRESS' => '',
];
$array['TRANSACTIONS']['WTRAN']['ITEMLINE'] = $timeline;
}
What I've tried
I cannot change 'SNHISTORY' => $grouped->toArray(), to something like $grouped->toArray(), under $timeline[$key][] = [ it will return error
I cannot add $array['TRANSACTIONS']['WTRAN']['ITEMLINE']['SNHISTORY'] = $grouped->toArray(); and remove 'SNHISTORY' => $grouped->toArray(), it will return error known bug
Question
How can I remove extra SNHISTORY around my data?
As mentioned in the comments section, you probably want to merge both arrays together:
$timeline[$key][] = [
'_attributes' => [
'operation' => 'Add'
],
'KeyID' => $barcode['product']['id'],
'ITEMNO' => $barcode['product']['sku'],
'QUANTITY' => '1',
'ITEMUNIT' => $barcode['product']['unit'],
'UNITRATIO' => '1',
'ITEMRESERVED1' => '',
'ITEMRESERVED2' => '',
'ITEMRESERVED3' => '',
'ITEMRESERVED4' => '',
'ITEMRESERVED5' => '',
'ITEMRESERVED6' => '',
'ITEMRESERVED7' => '',
'ITEMRESERVED8' => '',
'ITEMRESERVED9' => '',
'ITEMRESERVED10' => '',
'UNITPRICE' => $barcode['product']['price'],
'QTYCONTROL' => '0',
] + $grouped->toArray();
as i can see in your screen shot data
you are getting xml data you need to convert that into array
function xmlToArray($xml_string)
{
$doc = #simplexml_load_string($xml_string);
if ($doc) {
$xml = simplexml_load_string($xml_string);
$json = json_encode($xml);
return json_decode($json, true);
}
}
then u will get real array then u don't need to remove that as that is a parent key
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.
I have this array
$data = [
0 => [
'id' => '114',
'organization_name' => 'ABC Ltd',
'organization_telephone' => '01234 112233',
'organization_email' => 'admin#example.com',
'organization_url' => 'http://www.example.com',
'order_id' => '119',
'order_delivery_address_1' => '55',
'order_delivery_address_2' => 'High Street',
'order_delivery_address_3' => '',
'order_delivery_postcode' => 'LL27 0YX',
'product_colour_name' => 'Red',
'product_size_name' => '8/9',
'product_price' => '7.5',
'product_quantity' => '10',
'product_line_price' => '75',
],
];
And I want to divide it into 3 arrays as below:
$orgnization_details = [
'id' => '114',
'organization_name' => 'ABC Ltd',
'organization_telephone' => '01234 112233',
'organization_email' => 'admin#example.com',
'organization_url' => 'http://www.example.com',
]
$order_details = [
'order_id' => '119',
'order_delivery_address_1' => '55',
'order_delivery_address_2' => 'High Street',
'order_delivery_address_3' => '',
'order_delivery_postcode' => 'LL27 0YX',
]
$product_details = [
'product_colour_name' => 'Red',
'product_size_name' => '8/9',
'product_price' => '7.5',
'product_quantity' => '10',
'product_line_price' => '75',
]
I tried using array_filter for orgnization_details as below
foreach ($data as $details)
{
$orgnization_details = array_filter($details, function($key) { return $key <= 'organization_url'; }, ARRAY_FILTER_USE_KEY);
}
But it didn't work as expected.
Please help me with this.
You are very close.
The array_filter() needs to be modified to check if the beginning of the key is a specific string:
$orgnization_details = array_filter($details, function($key){
// do the first 13 chars equal "organization_" or is the key "id"?
return substr( $key, 0, 13 ) === 'organization_' || $key === 'id';
}, ARRAY_FILTER_USE_KEY);
// Do similar logic for setting $order_details
// Do similar logic for setting $product_details
You could define the keys and check for an intersection of the keys in the main array:
$orginazation_keys = array_flip(['id',
'organization_name',
'organization_telephone',
'organization_email',
'organization_url' ]);
$orgnization_details = array_intersect_key($data[0], $orginazation_keys);
Or you can grep for them since they follow a pattern:
$orgnization_details = array_intersect_key($data[0],
array_flip(preg_grep('/^(organization|id)/', array_keys($data[0]))));
<?php
$data = [
0 => [
'id' => '114',
'organization_name' => 'ABC Ltd',
'organization_telephone' => '01234 112233',
'organization_email' => 'admin#example.com',
'organization_url' => 'http://www.example.com',
'order_id' => '119',
'order_delivery_address_1' => '55',
'order_delivery_address_2' => 'High Street',
'order_delivery_address_3' => '',
'order_delivery_postcode' => 'LL27 0YX',
'product_colour_name' => 'Red',
'product_size_name' => '8/9',
'product_price' => '7.5',
'product_quantity' => '10',
'product_line_price' => '75',
],
];
$prefix_map = [
'id' => 'organization_details',
'organization' => 'organization_details',
'order' => 'order_details',
'product' => 'product_details'
];
foreach($data[0] as $k => $v) {
$key_prefix = explode('_', $k)[0];
$new_key = $prefix_map[$key_prefix];
$out[$new_key][$k] = $v;
}
extract($out);
var_export($organization_details);
Output:
array (
'id' => '114',
'organization_name' => 'ABC Ltd',
'organization_telephone' => '01234 112233',
'organization_email' => 'admin#example.com',
'organization_url' => 'http://www.example.com',
)
This creates a new multi-dimensional array with the corresponding associated keys by mapping the existing key prefixes (part before the underscore). Then it's just a case of using extract on that array to create the named variables.
This results in the three variables: $organization_details, $order_details and $product_details that you desire.
If your sub-arrays are consistently in the same order you can simply slice:
<?php
$data = [
0 => [
'id' => '114',
'organization_name' => 'ABC Ltd',
'organization_telephone' => '01234 112233',
'organization_email' => 'admin#example.com',
'organization_url' => 'http://www.example.com',
'order_id' => '119',
'order_delivery_address_1' => '55',
'order_delivery_address_2' => 'High Street',
'order_delivery_address_3' => '',
'order_delivery_postcode' => 'LL27 0YX',
'product_colour_name' => 'Red',
'product_size_name' => '8/9',
'product_price' => '7.5',
'product_quantity' => '10',
'product_line_price' => '75',
]
];
$first = $data[0];
$organisation_details = array_slice($first, 0, 5);
$order_details = array_slice($first, 5, 5);
$product_details = array_slice($first, -5);
var_export($organisation_details);
var_export($order_details);
var_export($product_details);
Output:
array (
'id' => '114',
'organization_name' => 'ABC Ltd',
'organization_telephone' => '01234 112233',
'organization_email' => 'admin#example.com',
'organization_url' => 'http://www.example.com',
)array (
'order_id' => '119',
'order_delivery_address_1' => '55',
'order_delivery_address_2' => 'High Street',
'order_delivery_address_3' => '',
'order_delivery_postcode' => 'LL27 0YX',
)array (
'product_colour_name' => 'Red',
'product_size_name' => '8/9',
'product_price' => '7.5',
'product_quantity' => '10',
'product_line_price' => '75',
)
I have an array, what Is like this:
$itemx=
[
'Weapons'=>[
'Sword'=> [
'ID' => '1',
'Name' => 'Lurker',
'Value' => '12',
'Made' => 'Acient'
],
'Shield'=> [
'ID' => '2',
'Name' => 'Obi',
'Value' => '22',
'Made' => 'Acient'
],
'Warhammer'=> [
'ID' => '3',
'Name' => 'Clotch',
'Value' => '124',
'Made' => 'Acient'
]
],
'Drinks'=>[
'Water'=> [
'ID' => '4',
'Name' => 'Clean-water',
'Value' => '1',
'Made' => 'Acient'
],
'Wine'=> [
'ID' => '5',
'Name' => 'Soff',
'Value' => '5',
'Made' => 'Acient'
],
'Vodka'=> [
'ID' => '6',
'Name' => 'Laudur',
'Value' => '7',
'Made' => 'Acient'
]
]
];
I want to find an array from it by Name or ID, so my output should be like this.
*Search by ID=4*
'Water'=> [
'ID' => '4',
'Name' => 'Clean-water',
'Value' => '1',
'Made' => 'Acient'
]
I look at other topics and found that I should use array_search
But It didn't work. I tried like this:
$arra=$itemx["Drinks"];
$key = array_search(4, array_column($arra, 'ID'));
var_dump($arra[$key]);
It also dident work when I tried with Name as a search key.
How can I get this working?
You can do it like below:-
$search_id = 4;
$final_array = [];
foreach($itemx as $key=>$val){
foreach($val as $k=>$v){
if($v['ID'] == $search_id){
$final_array[$k] = $itemx[$key][$k];
}
}
}
print_r($final_array);
https://eval.in/852123
This should probably get you what you want.
function rec($itemx,$search=4){
foreach ($itemx as $key => $value) {
if (is_array($value)) {
foreach ($value as $k => $v) {
if ($v['ID'] == $search) {
return $value;
}
}
}
}
}
print_r(rec($itemx,4));
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