dongzanghong4379 2016-08-17 15:08
浏览 54

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

I need an help to write a function that split this kind of array:

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

to all possible combinations without intersect values, for this example that must be resulting in two arrays:

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

The difficult is that must be working for any number of sub arrays and any number of values (products).

Here my function with missing part commented:

/**
 * @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;
}

input:

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

output:

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

If you can help me that will be very appreciated!

  • 写回答

1条回答 默认 最新

  • douhuang1973 2016-08-17 15:58
    关注

    Try this, $final_array is the array of all combination array: 'not perfect' because, it produce random result

    $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;
    
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题