Laravel 按数组分组 - 非法字符串偏移
Laravel group by array - illegal string offset
我已经将一个 csv 文件转换成数组,并用一些键对数组数据进行分组。我定义了一个 groupBy 函数来处理数组数据。但是,我遇到了这个错误,不确定是哪一部分出了问题。
代码:
分组函数
public function group($key, $item) {
$result = array();
foreach($item as $val) {
$result[$val[$key]][] = $val; <--error $key
}
return $result;
}
数据转换函数。
public function saveNewSale()
{
foreach (array_chunk($items, $chunk_size) as $chunk) {
$cleaned_items = [];
foreach ($chunk as $item) {
//Data Transformation
//call groupby function
$byGroup = $this->group('ref_num', $item);
array_push($cleaned_items, $item);
}
这是我的数组
array:2 [
0 => array:13 [
"ref_num" => "INV699"
"payment_term" => array:2 [
"id" => 1
"name" => "Cash"
]
"items" => array:1 [
0 => array:8 [
"code" => "3MVR0003"
"description" => "3M Vinyl
"tax_value" => 0.06
"qty" => 1
"unit" => 1
"price" => 10
"total" => 212
"total_tax" => 12
]
]
"terms" => array:1 [
0 => array:2 [
"date" => "2019-02-20"
"amount" => 200
]
]
]
1 => array:13 [
"ref_num" => "INV699"
"payment_term" => array:2 [
"id" => 1
"name" => "Cash"
]
"items" => array:1 [
0 => array:8 [
"code" => "sony"
"description" => "3xperia"
"tax_value" => 0.06
"qty" => 1
"unit" => 1
"price" => 10
"total" => 212
"total_tax" => 12
]
]
"terms" => array:1 [
0 => array:2 [
"date" => "2019-02-20"
"amount" => 200
]
]
]
]
我需要根据 ref_num
.
对这两个数组进行分组
我在 saveNewSale
中重写了您的代码并删除了 group
函数
public function saveNewSale($items)
{
$groupedArray = [];
foreach (array_chunk($items, 2) as $chunk)
{
foreach($chunk as $entry) {
$groupedArray[$entry['ref_num']][]= $entry;
}
}
return $groupedArray;
}
这里正在工作example
我已经将一个 csv 文件转换成数组,并用一些键对数组数据进行分组。我定义了一个 groupBy 函数来处理数组数据。但是,我遇到了这个错误,不确定是哪一部分出了问题。
代码: 分组函数
public function group($key, $item) {
$result = array();
foreach($item as $val) {
$result[$val[$key]][] = $val; <--error $key
}
return $result;
}
数据转换函数。
public function saveNewSale()
{
foreach (array_chunk($items, $chunk_size) as $chunk) {
$cleaned_items = [];
foreach ($chunk as $item) {
//Data Transformation
//call groupby function
$byGroup = $this->group('ref_num', $item);
array_push($cleaned_items, $item);
}
这是我的数组
array:2 [
0 => array:13 [
"ref_num" => "INV699"
"payment_term" => array:2 [
"id" => 1
"name" => "Cash"
]
"items" => array:1 [
0 => array:8 [
"code" => "3MVR0003"
"description" => "3M Vinyl
"tax_value" => 0.06
"qty" => 1
"unit" => 1
"price" => 10
"total" => 212
"total_tax" => 12
]
]
"terms" => array:1 [
0 => array:2 [
"date" => "2019-02-20"
"amount" => 200
]
]
]
1 => array:13 [
"ref_num" => "INV699"
"payment_term" => array:2 [
"id" => 1
"name" => "Cash"
]
"items" => array:1 [
0 => array:8 [
"code" => "sony"
"description" => "3xperia"
"tax_value" => 0.06
"qty" => 1
"unit" => 1
"price" => 10
"total" => 212
"total_tax" => 12
]
]
"terms" => array:1 [
0 => array:2 [
"date" => "2019-02-20"
"amount" => 200
]
]
]
]
我需要根据 ref_num
.
我在 saveNewSale
中重写了您的代码并删除了 group
函数
public function saveNewSale($items)
{
$groupedArray = [];
foreach (array_chunk($items, 2) as $chunk)
{
foreach($chunk as $entry) {
$groupedArray[$entry['ref_num']][]= $entry;
}
}
return $groupedArray;
}
这里正在工作example