结合多个数组PHP

时间:2015-09-04 16:11:01

标签: php arrays foreach

我需要使用一些数组并匹配它们,实际上我找到了一个解决方案,但这对我来说不是一个完美的解决方案,因为我没有3($数组)我如何使用打印另一个foreach的foreach ? :d

实际上我使用了这个解决方案:

//options
$array = ["13", "14", "15"];
$tutti = [
    "111" => "13", 
    "222" => "13", 
    "333" => "14", 
    "444" => "14", 
    "555" => "14", 
    "666" => "14", 
    "777" => "15", 
    "888" => "15"
];

$realarray = [];
$n_arrays = count($array); // 3 options

if ($n_arrays > 1) { // there is more of 1 option
    foreach ($tutti as $key => $value) {
        if ($value == $array[0]) {
            foreach ($tutti as $key2 => $value2) {
                if ($value2 != $array[0] && $value2 != $array[2]) {
                    foreach ($tutti as $key3 => $value3) {
                        if ($value3 != $array[0] && $value3 != $array[1]) {
                            echo "$key -> $key2 -> $key3<br>";
                            $realarray[] = "$key,$key2,$key3";
                        }
                    }
                }
            }
        }
    }
}

print_r($realarray);

1 个答案:

答案 0 :(得分:0)

尝试构建一个像这样的新数组:

$new_arr = Array();
foreach($tuttui AS $k => $v){
    $new_arr[$v][] = $k;
}

然后检查第一个数组中的$值是否在较大的数组中找到:

for($i=0; $i < count($array); $i++){
    if(in_array($array[$i], $new_arr[$array[$i]])){
         //Then do whatever you need
    }
}