2016-05-13 23 views
0

请有我阵列上的样子。所以,我需要按产品和尺寸对价格进行分组。意思是,在尺寸和产品相同的情况下获得价格总和,然后用这种格式显示在其他阵列中。产品|大小|价钱。comapare在阵列的两个相同的索引,然后得到第三个指标的总和

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),$密钥=> $值) {

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]; 
    } 

} 
+1

你试过了什么? –

+1

发生了什么日期 –

+0

日期是否必要?你可以假设所有的日期。 –

回答

0

伪代码:

  1. foreach($ initial_array as $ tmpArr){}
  2. 连接$ keyStr = $ tmpArr [0]。$ tmpArr [1];
  3. 与给定的$ keyStr
  4. 推$ tmpArr到$ newArray所有其他$ tmpArray-S您连接0,1和使用,作为ID来检查,​​如果你有在$ newArray那样的关键,
  5. 如果你有,你只是增加价格,如果你没有你推temparray与“ID”字符串。
  6. 后,你与所有完成,你可以在$ newArray的钥匙正好转换为数字
0

我真的真的不知道如果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" 
    } 
} 
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); 

Play with me on 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) 
     } 
    } 
    } 
} 

编辑:我还挺转移的规模,而不是复制,所以它不是最终产品$阵列英寸但是你会看到逻辑并有望自己调整代码。

相关问题