$orders = [
'yamada' => ['a','b','c'],
'tanaka' => ['b','f'],
'sato' => ['a','g','b','f'],
'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){
if(in_array($together, array_keys($data[$product]) )){
echo "yes";
foreach($data[$product] as $key => $value){
if($key == $together){
$data[$product][$key] += 1;
}
}
} else {
$data[$product] = array_merge($data[$product], array($together => 1));
}
// 一緒に買った商品の配列がない
} else {
$data[$product] = array($together => 1);
}
}
}
}
echo "<pre>";
var_dump($data);
echo "</pre>";
aを買ってる人でbを買ってる人は2人いる。
bを買ってる人で、aを買ってる人は2人、fを買ってる人は2人
array(6) {
[“a”]=>
array(4) {
[“b”]=>
int(2)
[“c”]=>
int(1)
[“g”]=>
int(1)
[“f”]=>
int(1)
}
[“b”]=>
array(4) {
[“a”]=>
int(2)
[“c”]=>
int(1)
[“f”]=>
int(2)
[“g”]=>
int(1)
}
[“c”]=>
array(2) {
[“a”]=>
int(1)
[“b”]=>
int(1)
}
[“f”]=>
array(4) {
[“b”]=>
int(2)
[“a”]=>
int(1)
[“g”]=>
int(1)
[“e”]=>
int(1)
}
[“g”]=>
array(3) {
[“a”]=>
int(1)
[“b”]=>
int(1)
[“f”]=>
int(1)
}
[“e”]=>
array(1) {
[“f”]=>
int(1)
}
きゃあああああああああああああああ、ほぼほぼ出来たくさい。
こっからさらにソートして出力したい。 OK、カモン