各人の注文データがある
$orders = [ 'yamada' => ['a','b','c'], 'tanaka' => ['b','f'], 'sato' => ['a','g'], 'goto' => ['e','f'], ]; foreach($orders as $order => $products){ foreach($products as $product){ echo $product; } echo "<br>"; }
abc
bf
ag
ef
こうすると、一緒に買われている商品が配列で入る。
$orders = [ 'yamada' => ['a','b','c'], 'tanaka' => ['b','f'], 'sato' => ['a','g','b'], 'goto' => ['e','f'], ]; foreach($orders as $order => $products){ foreach($products as $product){ $togethers = (array_diff($products, array($product))); foreach($togethers as $together){ if($data[$product] != null){ $data[$product] = array_merge($data[$product], array($together => 1)); } else { $data[$product] = array($together => 1); } } } }
array(6) {
[“a”]=>
array(3) {
[“b”]=>
int(1)
[“c”]=>
int(1)
[“g”]=>
int(1)
}
[“b”]=>
array(4) {
[“a”]=>
int(1)
[“c”]=>
int(1)
[“f”]=>
int(1)
[“g”]=>
int(1)
}
[“c”]=>
array(2) {
[“a”]=>
int(1)
[“b”]=>
int(1)
}
[“f”]=>
array(2) {
[“b”]=>
int(1)
[“e”]=>
int(1)
}
[“g”]=>
array(2) {
[“a”]=>
int(1)
[“b”]=>
int(1)
}
[“e”]=>
array(1) {
[“f”]=>
int(1)
}
}
keyの値を足して、一緒に買われている回数まで出したい。
おうおう、もうちょいだ