如何在laravel的数组列表中删除多余的数组
Posted
技术标签:
【中文标题】如何在laravel的数组列表中删除多余的数组【英文标题】:How to remove extra array in arrays list in laravel 【发布时间】:2020-12-19 07:08:37 【问题描述】:截图
如您所见,我的数据已加载到额外的 SNHISTORY
数组中,我需要删除该额外的数组。
代码
关于截图结果的行被注释了。
$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;
我尝试过的
-
我无法在
$timeline[$key][] = [
下将'SNHISTORY' => $grouped->toArray(),
更改为$grouped->toArray(),
之类的东西,它会返回错误
我无法添加$array['TRANSACTIONS']['WTRAN']['ITEMLINE']['SNHISTORY'] = $grouped->toArray();
和删除'SNHISTORY' => $grouped->toArray(),
它将返回错误known bug
问题
如何删除数据周围的额外SNHISTORY
?
【问题讨论】:
在您的屏幕截图中,您有需要转换为数组的 xml 数据$timeline[$key][] = [...] + $grouped->toArray();
?你想合并这些。
@KamleshPaul 是的,它正在将我的数据库数据转换为 xml,问题是我的数据不需要额外的 SNHISTORY
。
@Jeto 抱歉,我没听懂你想说什么
I mean like this.(抱歉,没有 SNHISTORY 键,已编辑)
【参考方案1】:
如 cmets 部分所述,您可能希望将两个数组合并在一起:
$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();
【讨论】:
【参考方案2】:正如我在您的屏幕截图中看到的那样data
您正在获取需要将其转换为数组的 xml 数据
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);
然后你会得到真正的数组然后你不需要删除它,因为它是一个父键
【讨论】:
感谢Jeto
solution已经修复了以上是关于如何在laravel的数组列表中删除多余的数组的主要内容,如果未能解决你的问题,请参考以下文章
如何删除 cookie 数组 Laravel 8 中的特定值