将具有交叉值的数组拆分为php中所有可能的数组组合

时间:2016-08-17 15:08:55

标签: php arrays combinations

我需要帮助来编写一个分割这种数组的函数:

array (size=2)
  seller1 => 
    array (size=2)
      0 => product1
      1 => product2
  seller2 => 
    array (size=2)
      0 => product1
      1 => product3

到没有相交值的所有可能组合,对于此示例,必须产生两个数组:

array (size=2)
  seller1 => 
    array (size=1)
      0 => product2
  seller2 => 
    array (size=2)
      0 => product1
      1 => product3

array (size=2)
  seller1 => 
    array (size=2)
      0 => product1
      1 => product2
  seller2 => 
    array (size=1)
      0 => product3

困难的是必须适用于任意数量的子阵列和任意数量的值(产品)

这里我的缺失部分功能评论:

/**
 * @param array $offersDataset
 * @param array $productsIds
 * @param int $sellers
 * @param array $previous
 *
 * @return array
 */
private function getCombinations($offersDataset, $productsIds, $sellers, $previous = array())
{
    $combinations = array();
    foreach ($offersDataset as $sellerId => $products) {
        if (false !== in_array($sellerId, array_keys($previous))) {
            continue;
        }
        $current = array($sellerId => $products);
        $total = $current + $previous;
        $remainingSellers = $sellers - 1;
        if ($remainingSellers > 0) {
            $deeper = $this->getCombinations($offersDataset, $productsIds, $remainingSellers, $total);
            $combinations = array_merge($combinations, $deeper);
        } else {
            $merge = array();
            foreach ($total as $satisfiable) {
                $merge = array_unique(array_merge($merge, $satisfiable));
            }
            $diff = array_diff($productsIds, $merge);
            if (empty($diff)) {
                $intersect = call_user_func_array('array_intersect', $total);
                //if (!empty($intersect)) {
                    // TODO
                    // HERE SPLIT TOTAL TO MULTIPLE ARRAYS
                //} else {
                    $combinations[] = $total;
                //}
            }
        }
    }

    return $combinations;
}

输入

$productsIds = array('product1', 'product2', 'product3');
$offersDataset = array(
  'seller1' => array('product1', 'product2'),
  'seller2' => array('product1', 'product3')
);
$sellers = 2;

输出

array (size=1)
  0 => 
    array (size=2)
      seller1 => 
        array (size=2)
          0 => product1
          1 => product2
      seller2 => 
        array (size=2)
          0 => product1
          1 => product3

如果你能帮助我,我将非常感激!

1 个答案:

答案 0 :(得分:0)

试试这个,$ final_array是所有组合数组的数组:'不完美'因为,它会产生随机结果

$arr = array('seller1' => array('0' => 'product1', '1' => 'product2', '2' => 'product3'),'seller2' => array('0' => 'product1', '1' => 'product3'));

$new_arr = [];
$final_array = [];
foreach ($arr as $key1 => $value1) {
    foreach ($value1 as $key2 => $value2) {
        $new_arr[$value2][] = $key1;
    }
}

$max_size = 1;
$combination = 1;

foreach ($new_arr as $key => $value) {
    if(count($value) > $max_size) {
        $max_size = count($value);
    }
    $combination = $combination * count($value);
}


for ($i=0; $i < $combination ; $i++) {
    foreach ($new_arr as $key => $value) {
        $k = array_rand($value);
        $v = $value[$k];
        $final_array[$i][$v][] = $key;
    }
}

echo "<pre>"; print_r($final_array); exit;