请查看我的数组。所以,我需要按产品和尺寸而不是价格来分组。意味着,在尺寸和产品相同的情况下,得到价格的总和,然后用这种格式显示在其他数组中。产品|尺寸|价格。
Array
(
[0] => Array
(
[0] => size
[1] => product
[2] => date
[3] => price
)
[1] => Array
(
[0] => Large
[1] => test2
[2] => 5/9/2016
[3] => 14
)
[2] => Array
(
[0] => small
[1] => test3
[2] => 5/10/2016
[3] => 17
)
[3] => Array
(
[0] => Large
[1] => test2
[2] => 5/9/2016
[3] => 17
)
[4] => Array
(
[0] => small
[1] => test
[2] => 5/10/2016
[3] => 1
)
[5] => Array
(
[0] => Large
[1] => test
[2] => 5/10/2016
[3] => 1
)
我试过这个
$counter = 0;
foreach(array_slice($top100SitesCSV,1)为$key=>$value){
if (isset($topSiteArray[$value[1]]['price']) && isset($topSiteArray[$value[1]]['size']) )
{
$topSiteArray[$value[1]]['price'] = $topSiteArray[$value[1]]['price'] + $value[3];
}
else
{
$topSiteArray[$value[1]]['price'] = $value[3];
$topSiteArray[$value[1]]['size'] = $value[0];
}
}
伪代码:
- foreach($initial_array为$tmpArr){}
- 连接$keyStr=$tmpArr[0]$tmpArr[1]
- 使用给定的$keyStr将$tmpArr推送到$newArray
- 对于所有其他$tmpArray-s,您连接0,1,并将其用作ID来检查$newArray中是否有类似的键
- 如果你有,你只需要增加价格,如果你没有,你就用"ID"字符串推送temparray
- 完成所有操作后,只需将$newArray的键转换为数字即可
我真的不确定u是否是这样的意思,但下面有一个简单的例子,它应该是一个很好的起点。应该添加一些数据验证,并且应该添加标签级数据表的动态映射。
$data = [
0 => [0 => 'size', 1 => 'product', 2 => 'date', 3 => 'price'],
1 => [0 => 'large', 1 => 'test', 2 => '5/9/2016', 3 => '5'],
2 => [0 => 'small', 1 => 'test1', 2 => '5/9/2016', 3 => '10'],
3 => [0 => 'large', 1 => 'test', 2 => '5/9/2016', 3 => '13'],
4 => [0 => 'small', 1 => 'test1', 2 => '5/9/2016', 3 => '22'],
5 => [0 => 'medium', 1 => 'test3', 2 => '5/9/2016', 3 => '5'],
];
$labels = $data[0]; // extract labels from data table
unset($data[0]); // delete extracted data
$result = []; // prepare results container
foreach($data as $product){ // loop through all products
if(!isset($result[$product[1]])){ // if prod isn't in table create new result row
$result[$product[1]] = ['size' => $product[0], 'item' => 1, 'price' => $product[3]];
} else { // if it`s just increment item count and calculate total price
$result[$product[1]]['price'] += $product[3];
$result[$product[1]]['item'] += 1;
}
}
var_dump($result); // you can filter out of that table results for less than 2 items, or just don`t save it to the result in above loop
结果
array(3) {
["test"]=>
array(3) {
["size"]=>
string(5) "large"
["item"]=>
int(2)
["price"]=>
int(18)
}
["test1"]=>
array(3) {
["size"]=>
string(5) "small"
["item"]=>
int(2)
["price"]=>
int(32)
}
["test3"]=>
array(3) {
["size"]=>
string(6) "medium"
["item"]=>
int(1)
["price"]=>
string(1) "5"
}
}
这就是我如何理解您描述不周的请求:
代码
<?php
error_reporting(E_ALL);
$raw = array(
[ 'size', 'product', 'date', 'price' ],
[ 'Large', 'test2', '5/9/2016', 14 ],
[ 'small', 'test3', '5/10/2016', 17 ],
[ 'Large', 'test2', '5/9/2016', 17 ],
[ 'small', 'test', '5/10/2016', 1 ],
[ 'Large', 'test', '5/10/2016', 1 ]
);
$keys = array_shift($raw);
$products = array();
foreach ($raw as $item) {
$item = array_combine($keys, $item);
$size = array_shift($item);
$product = array_shift($item);
$products[$product][$size][] = $item;
}
var_dump($products);
在3v4l.org 上与我一起玩
输出
array(3) {
["test2"]=> array(1) {
["Large"]=> array(2) {
[0]=> array(2) {
["date"]=> string(8) "5/9/2016"
["price"]=> int(14)
}
[1]=> array(2) {
["date"]=> string(8) "5/9/2016"
["price"]=> int(17)
}
}
}
["test3"]=> array(1) {
["small"]=> array(1) {
[0]=> array(2) {
["date"]=> string(9) "5/10/2016"
["price"]=> int(17)
}
}
}
["test"]=> array(2) {
["small"]=> array(1) {
[0]=> array(2) {
["date"]=> string(9) "5/10/2016"
["price"]=> int(1)
}
}
["Large"]=> array(1) {
[0]=> array(2) {
["date"]=> string(9) "5/10/2016"
["price"]=> int(1)
}
}
}
}
编辑:我有点改变了大小,而不是复制,所以它不在最终的$products数组中。但是您看到了其中的逻辑,并有望自己调整代码。