将数组内容插入对象不起作用 [英] Inserting contents of array into object not working
问题描述
我正在创建带有产品系列参数的发票对象。由于一个订单可以包含很多产品,因此我使用的是一个迭代器,该迭代器收集一系列订单项。 $ prod_line是一个保存产品线数组集合的数组。
I'm creating an invoice object that takes a product line parameter. Because an order can have many products, I'm using an iterator that collects an array of line items. $prod_line is an array that holds the collection of product line arrays.
我希望能够将$ prod_line的内容注入到对象中,但是我只使用$ prod_line [0]或$ prod_line [1]时会成功。
I was hoping I would be able to inject the contents of $prod_line into my object, but I only have success when using $prod_line[0] or $prod_line[1].
我尝试了一些技巧来删除父层数组,例如array_merge_recursive-但我在这个愚蠢的问题上呆了几个小时,没有运气,真的需要进步。
I have tried a few techniques to remove the parent layer array, such as array_merge_recursive -- but I've been stuck on this silly issue for a few hours with no luck and really need to progress.
我们将不胜感激!
注意:下面的代码已精简易读-忽略丢失变量实例化和用于概念。
NOTE: code below is stripped down for readability -- ignore missing variable instantiation and use for concept.
function create_qb_invoice()
{
foreach ($az as $order_id => $order) // FOR EACH ORDER
{
$line_id = 1;
foreach ($order['products'] as $product) // FOR EACH PRODUCTS
{
// A 'LINE' STANDS FOR AN ITEMIZED PRODUCT LINE IN QUICKBOOKS INVOICE FORMS
// FOR EACH PRODUCT, CREATE A LINE FOR QUICKBOOKS INVOICE
$prod_line[] =
[
"Id" => $line_id,
"LineNum" => $line_id,
"Amount" => $product['ItemPrice']['Amount'],
"DetailType" => "SalesItemLineDetail",
"SalesItemLineDetail" => [
"ItemRef" => [
"value" => $theItemId,
"name" => $theItemName
],
"TaxCodeRef" => [
"value" => "NON"
],
"UnitPrice" => $product['ItemPrice']['Amount'],
"Qty" => $product['QuantityOrdered'],
]
];
$line_id ++;
}
$myInvoiceObj = Invoice::create([
"LinkedTxn" => [],
"Line" =>
[
// THIS SHOULD BE REPLACED WITH THE $prod_line VARIABLE
// HOWEVER I'VE ONLY BEEN ABLE TO USE $prod_line[0], OR $prod_line[1]
// REGULAR $prod_line HAS AN EXTRA LAYER OF ARRAY THAT Invoice::create WON'T BE EXPECTING
// STRIPPING AWAY THE CONTAINING ARRAY IS NOT WORKING WHEN i USE array_merge_recursive or similar solutions
// [
// "Id" => "1",
// "LineNum" => 1,
// "Amount" => $order['ItemPrice']['Amount'],
// "DetailType" => "SalesItemLineDetail",
// "SalesItemLineDetail" =>
// [
// "ItemRef" =>
// [
// "value" => $theItemId,
// "name" => $theItemName
// ],
// "TaxCodeRef" =>
// [
// "value" => "NON"
// ]
// ]
// ],
[
"Amount" => $order['OrderTotal']['Amount'],
"DetailType" => "SubTotalLineDetail",
"SubTotalLineDetail" => []
]
],
"CustomerRef" =>
[
"value" => "18269",
"name" => "Amazon"
]
]);
$resultingInvoiceObj = $dataService->Add($myInvoiceObj);
}//for
}
$ prod_line的var转储
var dump of $prod_line
array(2) { // this array layer should be stripped out
[0]=>
array(5) { // and this array-- just want to pass the inner contents
["Id"]=>
int(1)
["LineNum"]=>
int(1)
["Amount"]=>
string(6) "366.97"
["DetailType"]=>
string(19) "SalesItemLineDetail"
["SalesItemLineDetail"]=>
array(4) {
["ItemRef"]=>
array(2) {
["value"]=>
string(4) "9378"
["name"]=>
string(20) "Countryman E6OW5C2SR"
}
["TaxCodeRef"]=>
array(1) {
["value"]=>
string(3) "NON"
}
["UnitPrice"]=>
strin
g(6) "366.97"
["Qty"]=>
string(1) "1"
}
}
[1]=>
array(5) {
["Id"]=>
int(2)
["LineNum"]=>
int(2)
["Amount"]=>
string(6) "365.81"
["DetailType"]=>
string(19) "SalesItemLineDetail"
["SalesItemLineDetail"]=>
array(4) {
["ItemRef"]=>
array(2) {
["value"]=>
string(4) "9392"
["name"]=>
string(20) "Countryman E6OW5T2SR"
}
["TaxCodeRef"]=>
array(1) {
["value"]=>
string(3) "NON"
}
["UnitPrice"]=>
string(6) "365.81"
["Qty"]=>
string(1) "1"
}
}
}
推荐答案
一旦您填充 $ prod_line
项,就会另外执行 $ prod_line []
添加 Amount
数组。不是因为它剥离了一个数组,而是因为它的结构不正确。
Once you populate $prod_line
items do an additional $prod_line[]
to add the Amount
, array. Its not because its an array that it strips it it's because its not the correct structure.
您本质上在做:
<?php
$prod_line = [
['key' => 'value'],
['key' => 'value']
];
$final = [
$prod_line,
[
'key' => 'value'
]
];
将产生以下内容:
Array
(
[0] => Array
(
[0] => Array
(
[key] => value
)
[1] => Array
(
[key] => value
)
)
[1] => Array
(
[key] => value
)
)
这不是正确的结构。
相反,您应该在进行过程中构建最终阵列。
Instead you should be building your final array as you go along.
<?php
function create_qb_invoice()
{
foreach ($az as $order_id => $order) // FOR EACH ORDER
{
$prod_line = [];
$line_id = 1;
foreach ($order['products'] as $product) // FOR EACH PRODUCTS
{
$prod_line[] =
[
"Id" => $line_id,
"LineNum" => $line_id,
"Amount" => $product['ItemPrice']['Amount'],
"DetailType" => "SalesItemLineDetail",
"SalesItemLineDetail" => [
"ItemRef" => [
"value" => $theItemId,
"name" => $theItemName
],
"TaxCodeRef" => [
"value" => "NON"
],
"UnitPrice" => $product['ItemPrice']['Amount'],
"Qty" => $product['QuantityOrdered'],
]
];
$line_id++;
}
$prod_line[] = [
"Amount" => $order['OrderTotal']['Amount'],
"DetailType" => "SubTotalLineDetail",
"SubTotalLineDetail" => []
]
$myInvoiceObj = Invoice::create([
"LinkedTxn" => [],
"Line" => $prod_line,
"CustomerRef" =>
[
"value" => "18269",
"name" => "Amazon"
]
]);
$resultingInvoiceObj = $dataService->Add($myInvoiceObj);
}//for
}
可以在此处(下半部分)找到此调用的正确数组结构:
The correct array structure of this call can be found here (halfway down):
$myInvoiceObj = Invoice::create([
"DocNumber" => "1070",
"LinkedTxn" => [],
"Line" => [[
"Id" => "1",
"LineNum" => 1,
"Amount" => 150.0,
"DetailType" => "SalesItemLineDetail",
"SalesItemLineDetail" => [
"ItemRef" => [
"value" => "1",
"name" => "Services"
],
"TaxCodeRef" => [
"value" => "NON"
]
]
], [
"Amount" => 150.0,
"DetailType" => "SubTotalLineDetail",
"SubTotalLineDetail" => []
]],
"CustomerRef" => [
"value" => "1",
"name" => "Amy's Bird Sanctuary"
]
]);
$resultingInvoiceObj = $dataService->Add($myInvoiceObj);
这篇关于将数组内容插入对象不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!