1

我想获取数组的组合键 - 这里是示例:

$keys = array(
        array(
            'original',
            'section 1',
            'section 2'
        ),
        array(
            'original',
            'section 1'
        ),
        array(
            'original',
            'section 1'
        ),
        array(
            'original',
            'section 1',
            'section 2'
        ),
        array(
            'original',
            'section 1'
        )
);

所有原始部分都是 00000(5 个数字,因为有 5 个不同的数组,但可以有 1 到 n 个数组,不限于 5 个),变体 #15 将是例如 01010。我基本上想获得所有变体的列表带有该键的列表以获取它们。我们假设每个数组内不超过 9 个部分 + 原始部分。

输出应如下所示:

array(
    array(
        "key" => "00000",
        "data" => array(
            "original", "original", "original", "original", "original"
        )
    ),
    array(
        "key" => "10000",
        "data" => array(
            "section 1", "original", "original", "original", "original"
        )
    ),
    array(
        "key" => "20000",
        "data" => array(
            "section 2", "original", "original", "original", "original"
        )
    ),
    array(
        "key" => "01000",
        "data" => array(
            "original", "section 1", "original", "original", "original"
        )
    )
    [...]
)  

除此之外,如果有一个很好的方法来获得它,那将是很好的。给出密钥 01010 并取回所有部分。

谢谢!

4

1 回答 1

2

这是我想出的:

$options = array();
$counter1 = 0;
foreach($keys as $values) {
    $counter1++;
    $counter2 = 0;
    $options[$counter1] = array();
    foreach($values as $key => $value) {
        if(isset($options[($counter1 - 1)])) {
            foreach($options[($counter1 - 1)] as $old_val) {
                $counter2++;
                $old_array = $old_val['data'];
                array_push($old_array, $value);
                $options[$counter1][$counter2] = array();
                $options[$counter1][$counter2]['key'] = $old_val['key'] . $key;
                $options[$counter1][$counter2]['data'] = $old_array;
            }
        }
        else {
            $counter2++;
            $options[$counter1][$counter2] = array();
            $options[$counter1][$counter2]['key'] = $key;
            $options[$counter1][$counter2]['data'] = array($value);
        }
    }
}
$all_options = end($options);

你得到 72 个正确的结果,因为它是 3 * 2 * 2 * 3 * 2 = 72

于 2013-01-08T15:50:41.927 回答