我需要帮助组织我的库存数组。该结构是一个大型库存数组,其中包含一系列项目。每个项目数组包含以下内容: item,item_group,item_no和已售出的数组。出售包括日期和数量的内部数组。现在,我无法为我需要的输出组织它。我会给你们一些输入和输出的样本。所以请检查,非常感谢。
我的$ inventory数组的样本部分
Array
(
[0] => Array
(
[item] => NK
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 11
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 1
[date] => 2017-10-29
)
)
[item_no] => 1
)
[1] => Array
(
[item] => FL
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 7
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 2
[date] => 2017-10-29
)
)
[item_no] => 2
)
[2] => Array
(
[item] => AD
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 5
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 3
[date] => 2017-10-29
)
)
[item_no] => 3
)
[3] => Array
(
[item] => CV
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 4
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 6
[date] => 2017-10-29
)
)
[item_no] => 4
)
[4] => Array
(
[item] => NB
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 12
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 4
[date] => 2017-10-29
)
)
[item_no] => 5
)
[5] => Array
(
[item] => SP
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 4
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 6
[date] => 2017-10-29
)
)
[item_no] => 6
)
[6] => Array
(
[item] => WB
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 5
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 2
[date] => 2017-10-29
)
)
[item_no] => 7
)
[7] => Array
(
[item] => wny
[item_group] => 12
[sold] => Array
(
[0] => Array
(
[quantity] => 4
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 6
[date] => 2017-10-29
)
)
[item_no] => 8
)
[8] => Array
(
[item] => bs
[item_group] => 12
[sold] => Array
(
[0] => Array
(
[quantity] => 15
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 2
[date] => 2017-10-29
)
)
[item_no] => 9
)
[9] => Array
(
[item] => st
[item_group] => 12
[sold] => Array
(
[0] => Array
(
[quantity] => 16
[date] => 2017-10-29
)
)
[item_no] => 10
)
[10] => Array
(
[item] => ayhtdws
[item_group] => 12
[sold] => Array
(
[0] => Array
(
[quantity] => 2
[date] => 2017-10-29
)
)
[item_no] => 11
)
[11] => Array
(
[item] => sif
[item_group] => 12
[sold] => Array
(
[0] => Array
(
[quantity] => 3
[date] => 2017-10-29
)
)
[item_no] => 12
)
[12] => Array
(
[item] => bb
[item_group] => 12
[sold] => Array
(
[0] => Array
(
[quantity] => 6
[date] => 2017-10-29
)
)
[item_no] => 13
)
)
从那里,我想要展示的是这样的。按日期升序分组。并且每个项目=>已售出数量
Array
(
[0] => Array
(
[date] => 2017-10-28
[NK] => 11
[FL] => 7
[AD] => 5
[CV] => 4
[NB] => 12
[SP] => 4
[WB] => 5
[wny] => 4
[bs] => 15
)
[1] => Array
(
[date] => 2017-10-29
[NK] => 1
[FL] => 2
[AD] => 3
[CV] => 6
[NB] => 4
[SP] => 6
[WB] => 2
[wny] => 6
[bs] => 2
[st] => 16
[ayhtdws] => 2
[sif] => 3
[bb] => 6
)
)
我花了差不多3天的时间来解决这个问题,直到写这篇文章,我才能做到这一点
$result = array();
$dates = array();
foreach ($inventory as $key => $item) {
foreach ($item['sold'] as $k => $v) {
array_push($dates, $v['date']);
}
}
$dates = array_unique($dates);
foreach($dates as $key => $value) {
array_push($result, array('date' => $value));
}
foreach ($dates as $rkey => $rvalue) {
foreach ($inventory as $key => $item) {
foreach ($item['sold'] as $k => $v) {
if ($v['date'] = $result[$key]['date']) {
array_push($result[$key][$item['item']] = $v['quantity']);
}
}
}
}
return $result;
这当然给了我这个悲伤的结果
Array
(
[0] => Array
(
[date] => 2017-10-28
[NK] => 1
)
[1] => Array
(
[date] => 2017-10-29
[FL] => 2
)
)
更糟糕的是,我们有关于圈复杂度的规则,我们应该只有最多3个循环/条件,每个循环/条件最多3个嵌套级别。并且整个组织不应该有任何用户创建的功能。
即使不遵守规则,我也无法解决好几天。对不起,如果问题很长。请帮忙:(
更新:var_export($ inventory)输出
array (
0 =>
array (
'item' => 'NK',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '11',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '1',
'date' => '2017-10-29',
),
),
'item_no' => '1',
),
1 =>
array (
'item' => 'FL',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '7',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '2',
'date' => '2017-10-29',
),
),
'item_no' => '2',
),
2 =>
array (
'item' => 'AD',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '5',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '3',
'date' => '2017-10-29',
),
),
'item_no' => '3',
),
3 =>
array (
'item' => 'CV',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '4',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '6',
'date' => '2017-10-29',
),
),
'item_no' => '4',
),
4 =>
array (
'item' => 'NB',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '12',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '4',
'date' => '2017-10-29',
),
),
'item_no' => '5',
),
5 =>
array (
'item' => 'SP',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '4',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '6',
'date' => '2017-10-29',
),
),
'item_no' => '6',
),
6 =>
array (
'item' => 'WB',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '5',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '2',
'date' => '2017-10-29',
),
),
'item_no' => '7',
),
7 =>
array (
'item' => 'wny',
'item_group' => '12',
'sold' =>
array (
0 =>
array (
'quantity' => '4',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '6',
'date' => '2017-10-29',
),
),
'item_no' => '8',
),
8 =>
array (
'item' => 'bs',
'item_group' => '12',
'sold' =>
array (
0 =>
array (
'quantity' => '15',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '2',
'date' => '2017-10-29',
),
),
'item_no' => '9',
),
9 =>
array (
'item' => 'st',
'item_group' => '12',
'sold' =>
array (
0 =>
array (
'quantity' => '16',
'date' => '2017-10-29',
),
),
'item_no' => '10',
),
10 =>
array (
'item' => 'ayhtdws',
'item_group' => '12',
'sold' =>
array (
0 =>
array (
'quantity' => '2',
'date' => '2017-10-29',
),
),
'item_no' => '11',
),
11 =>
array (
'item' => 'sif',
'item_group' => '12',
'sold' =>
array (
0 =>
array (
'quantity' => '3',
'date' => '2017-10-29',
),
),
'item_no' => '12',
),
12 =>
array (
'item' => 'bb',
'item_group' => '12',
'sold' =>
array (
0 =>
array (
'quantity' => '6',
'date' => '2017-10-29',
),
),
'item_no' => '13',
),
)
答案 0 :(得分:1)
我不知道这是否与Erwin的代码差不多,但是我在13小时前编写了这段代码,不得不等待阵列。
编辑;我测试了Erwin的代码,似乎我们已经接近匹配的代码了。 他做了一个循环来获取日期,但它或多或少相同。
我循环播放阵列并销售子阵列 我将新数组键设为日期示例:
Echo $new['2017-10-28']['nk']; // 11
如果没有设置日期键,我就创建它。
循环完成后,我使用array_values删除日期键,使数组看起来像:
Echo $new[0]['nk']; // 11
代码:
$new =[];
Foreach($inventory as $sub){
Foreach($sub["sold"] as $sold){
If (!isset($new[$sold["date"]]["date"])) $new[$sold["date"]]["date"] = $sold["date"];
$new[$sold["date"]][$sub["item"]] = $sold["quantity"];
}
}
$new = array_values($new);
Var_dump($new);
答案 1 :(得分:0)
我已经完成了一些测试和解决方法,并提出了这个解决方案。我设法使用ksort()
,array_values()
和array_key_exists()
来实现这一目标。我首先使用日期作为数组键来包含该日期销售的所有商品。然后,在收集所有项目后将其移动到数组内部,然后重新编号数组。通过代码注释进一步解释
// result array
$result = array();
// Loop for each item on inventory
foreach ($inventory as $inv) {
// Loop for each sold array
foreach ($inv['sold'] as $item) {
// date sold
$date = $item['date'];
// Check if date already exist in result array as key
// If not, Create new array with key equal to date and push to result array
if (!array_key_exists($date, $result)) {
$result[$date] = array();
}
// Add array of item => quantity to corresponding date array inside result array
$result[$date][$inv['item']] = $item['quantity'];
}
}
// From here you already have each item => quantity sold inside each date array
// e.g. $result = array( '2017-10-28' => array('NK' => '11', 'FL' => '7', ...) );
// Sort keys which are dates ascending
ksort($result);
// Loop to results and set
foreach ($result as $key => $value) {
$result[$key]['date'] = $key;
}
// Renumber Keys
$result = array_values($result);
// Print result
echo '<pre>';
print_r($result);
输出
Array
(
[0] => Array
(
[NK] => 11
[FL] => 7
[AD] => 5
[CV] => 4
[NB] => 12
[SP] => 4
[WB] => 5
[wny] => 4
[bs] => 15
[date] => 2017-10-28
)
[1] => Array
(
[NK] => 1
[FL] => 2
[AD] => 3
[CV] => 6
[NB] => 4
[SP] => 6
[WB] => 2
[wny] => 6
[bs] => 2
[st] => 16
[ayhtdws] => 2
[sif] => 3
[bb] => 6
[date] => 2017-10-29
)
)