PHP数组从多重数组中获取所有可能的变体

时间:2015-04-14 08:19:39

标签: php arrays sorting

这里已经有一些像这样的帖子,但是没有他们帮我, 我有一个看起来像这样的示例数组

$array = array(
array(
    'label' => 'test 1-1',
    'pricing_value' => '10',
    'attr_id' => '1',
    'selectionId' => '2',
    'required' => '1'
),
array(
    'label' => 'test 1-2',
    'pricing_value' => '10',
    'attr_id' => '1',
    'selectionId' => '12',
    'required' => '1'
),
array(
    'label' => 'test 1-3',
    'pricing_value' => '10',
    'attr_id' => '1',
    'selectionId' => '132',
    'required' => '1'
),
array(
    'label' => 'test 2-1',
    'pricing_value' => '10',
    'attr_id' => '2',
    'selectionId' => '11',
    'required' => '0'
),
array(
    'label' => 'test 2-2',
    'pricing_value' => '10',
    'attr_id' => '1',
    'selectionId' => '14',
    'required' => '0'
),
array(
    'label' => 'test 2-3',
    'pricing_value' => '10',
    'attr_id' => '2',
    'selectionId' => '12',
    'required' => '0'
),

);

输出应该像

array (
array (
'label' => "test 1-1 + test 2-1",
'price-value' => '20',
'required' => '1'
)
)

依旧...... 但没有任何重复,如测试2-1 +测试1-1将是重复 数组被分组' attr_id'并且可以有100个像 Sesction_id是这个元素id,它们在一个attr_id中不会是相同的部分ID,但是如果attr_id不相同它们可以是相同的。希望它有意义。

如果需要设置为0,那么它们应该是2options with this and without it

喜欢" required1 + required2 + not required3"和" required1 + required2"

我在排序数组方面真的很糟糕有人可以帮我这个吗?

编辑:

所有这些选项都是从两个数组合并为一个 每个数组id都在attr_id中,因此在一个变体中不能有相同的对象,就像没有测试1-1 + test 1-2

代码需要输出所有可能的变化 在这个例子中,它将像

test 1-1 + test 2-1 |
test 1-1 + test 2-2 |
test 1-1 + test 2-3 |

test 1-2 + test 2-1 |
test 1-2 + test 2-2 |
test 1-2 + test 2-3 |

test 1-3 + test 2-1 |
test 1-3 + test 2-2 |
test 1-3 + test 2-3 |

$array = array(
array(
    'label' => 'test 1-1 + test 2-1',
    'pricing_value' => '20'
),
array(
    'label' => 'test 1-1 + test 2-2',
    'pricing_value' => '20'
),
array(
    'label' => 'test 1-1 + test 2-3',
    'pricing_value' => '20'
),

array(
    'label' => 'test 1-2 + test 2-1',
    'pricing_value' => '20'
),
array(
    'label' => 'test 1-2 + test 2-2',
    'pricing_value' => '20'
),
array(
    'label' => 'test 1-2 + test 2-3',
    'pricing_value' => '20'
),
array(
    'label' => 'test 1-3 + test 2-1',
    'pricing_value' => '20'
),
array(
    'label' => 'test 1-3 + test 2-2',
    'pricing_value' => '20'
),
array(
    'label' => 'test 1-3 + test 2-3',
    'pricing_value' => '20'
),
array(
    'label' => 'test 1-1',
    'pricing_value' => '10'
),
array(
    'label' => 'test 1-2',
    'pricing_value' => '10'
),
array(
    'label' => 'test 1-3',
    'pricing_value' => '10'
),

);

并且还需要test-1所有元素,因为不需要test-2

test 1-1 |
test 1-2 |
test 1-3 |

希望它会有所帮助

1 个答案:

答案 0 :(得分:0)

尝试这个,让我知道这是否是你想要的:

        <?php 
$array = array(
array(
    'label' => 'test 1-1',
    'pricing_value' => '10',
    'attr_id' => '1',
    'selectionId' => '2',
    'required' => '1'
),
array(
    'label' => 'test 1-2',
    'pricing_value' => '10',
    'attr_id' => '1',
    'selectionId' => '12',
    'required' => '1'
),
array(
    'label' => 'test 1-3',
    'pricing_value' => '10',
    'attr_id' => '1',
    'selectionId' => '132',
    'required' => '1'
),
array(
    'label' => 'test 2-1',
    'pricing_value' => '10',
    'attr_id' => '2',
    'selectionId' => '11',
    'required' => '0'
),
array(
    'label' => 'test 2-2',
    'pricing_value' => '10',
    'attr_id' => '1',
    'selectionId' => '14',
    'required' => '0'
),
array(
    'label' => 'test 2-3',
    'pricing_value' => '10',
    'attr_id' => '2',
    'selectionId' => '12',
    'required' => '0'
)
);

$filteredArray=array();
$count=0;
foreach($array as $key=>$value){
        $filteredArray[$value['required']][$count]['label']=$value['label'];
        $filteredArray[$value['required']][$count]['pricing_value']=$value['pricing_value'];
        $count++;
}

$finalArray=array();
foreach($filteredArray[1] as $key=>$value){
    if(!isset($finalArray[$value['label']])){
        $finalArray[$value['label']]=$value['pricing_value'];
    }
    foreach($filteredArray[0] as $key=>$value1){
        if(!isset($finalArray[$value['label']."+".$value1['label']])){
            $finalArray[$value['label']."+".$value1['label']]=$value['pricing_value']+$value1['pricing_value'];
        }
    }
}
print_r($finalArray);die;
?>

输出:

   Array
(
    [test 1-1] => 10
    [test 1-1+test 2-1] => 20
    [test 1-1+test 2-2] => 20
    [test 1-1+test 2-3] => 20
    [test 1-2] => 10
    [test 1-2+test 2-1] => 20
    [test 1-2+test 2-2] => 20
    [test 1-2+test 2-3] => 20
    [test 1-3] => 10
    [test 1-3+test 2-1] => 20
    [test 1-3+test 2-2] => 20
    [test 1-3+test 2-3] => 20
)