2016-05-13 3 views
0

Bitte schauen Sie auf mein Array. Also, ich muss es nach Produkt und Größe über Preis gruppieren. Bedeutet, wo die Größe und das Produkt gleich sind, erhalten Sie die Summe der Preise und zeigen Sie dann in anderem Array mit diesem Format. Produkt | Größe | Preis.comapare die zwei gleichen Indizes in Array und dann die Summe der dritten Index

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 
    ) 

Ich habe versucht, diese

$counter = 0; 

foreach (array_slice ($ top100SitesCSV, 1) als $ 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]; 
    } 

} 
+1

was haben Sie ausprobiert? –

+1

Was passiert mit dem Datum? –

+0

Datum ist nicht erforderlich? Sie können alle Daten übernehmen. –

Antwort

0

Pseudocode:

  1. foreach ($ initial_array als $ tmpArr) {}
  2. Verketten $ keyStr = $ tmpArr [0]. $ TmpArr [1];
  3. Drücken Sie die $ tmpArr zu einem $ newArray mit dem angegebenen $ keyStr
  4. für alle anderen $ tmpArray-s Sie verketten 0,1 und verwenden, die als ID zu überprüfen, ob Sie eine Taste wie in der $ newArray haben,
  5. Wenn Sie haben, erhöhen Sie nur den Preis, wenn Sie nicht haben, drücken Sie das Temparray mit dieser 'ID' Zeichenfolge.
  6. Nachdem Sie alle beenden können Sie konvertieren nur die Schlüssel des $ newArray Zahlen
0

i`m nicht wirklich sicher, ob u so etwas wie dies bedeutete, aber unter Ihnen einfaches Beispiel haben, sollte es gut sein Startpunkt. Es sollte eine Datenvalidierung hinzugefügt werden und Sie sollten eine dynamische Zuordnung Ihrer Label vel Datentabelle hinzufügen.

$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 

Ergebnis

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

Dies ist, wie ich Ihre schlecht beschrieben Anfrage verstanden:

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

OUTPUT

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) 
     } 
    } 
    } 
} 

EDIT: Ich verlagerte irgendwie die Größe anstelle des Kopierens, so dass es nicht in der letzten Reihe $ Produkte. aber Sie sehen die Logik und können den Code hoffentlich selbst anpassen.

Verwandte Themen