3v4l.org

run code in 200+ PHP versions simultaneously
<?php $data = [ ['alteration1', 'alteration4',], ['alteration2','alteration3'], ['alteration2','alteration3'], [] ]; $combinations = [[]]; foreach($data as $index => $current_data){ $current_data = array_unique($current_data); if(empty($current_data)) continue; $temp_combinations = []; foreach($current_data as $value){ foreach($combinations as $each_combination){ $temp_combinations[] = array_merge($each_combination,[$value]); } } $combinations = $temp_combinations; } $set = []; $unique_combinations = []; foreach($combinations as $each_combination){ natsort($each_combination); $serialized_form = implode(",",$each_combination); if(isset($set[$serialized_form])) continue; if(empty($each_combination)) continue; $unique_combinations[] = $each_combination; $set[$serialized_form] = true; } print_r($unique_combinations);
Output for 7.1.25 - 7.4.0
Array ( [0] => Array ( [0] => alteration1 [1] => alteration2 [2] => alteration2 ) [1] => Array ( [2] => alteration2 [1] => alteration2 [0] => alteration4 ) [2] => Array ( [0] => alteration1 [2] => alteration2 [1] => alteration3 ) [3] => Array ( [2] => alteration2 [1] => alteration3 [0] => alteration4 ) [4] => Array ( [0] => alteration1 [1] => alteration3 [2] => alteration3 ) [5] => Array ( [2] => alteration3 [1] => alteration3 [0] => alteration4 ) )