请看看我的阵列。所以,我需要按产品和尺寸对价格进行分组。手段,大小和产品相同的地方得到价格的总和,然后用这种格式显示在其他数组中。产品|尺寸|价。
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)as $ 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];
}
}
答案 0 :(得分:0)
伪代码:
答案 1 :(得分:0)
我不确定你是否意味着这样的事情,但是下面你有一个简单的例子,它应该是一个很好的起点。应该添加一些数据验证,你应该添加标签vel数据表的动态映射。
$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"
}
}
答案 2 :(得分:0)
这就是我理解您描述不佳的请求的方式:
<强> CODE 强>
<?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);
<强>输出强>
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数组中。但你看到了逻辑,并希望自己调整代码。