PHP:没有Nead两次数组循环

时间:2013-11-18 10:04:31

标签: php arrays loops foreach

您好我有一个构建变体但循环次数太多的函数

这是函数

function combine_array($a){
    $out = array();
    if (count($a) == 1) {
        $x = array_shift($a);
        foreach ($x as $v) $out[] = array($v);
        return $out;
    }
    foreach ($a as $k => $v){
        $b = $a;
        unset($b[$k]);
        $x = combine_array($b);
        foreach ($v as $v1){
            foreach ($x as $v2) 
            $out[] = array_merge(array($v1), $v2);
        }
    }
    return $out;
}

这是数组数据

$data = array (
    array (
        'Size',
        'variation_id' => array (
            'S',
            'M',
        )
    ),
    array (
        'Length',
        'variation_id' => array (
            '14 Inches',
            '18 Inches',
        )
    )
);

$check_var_id = function($value){
    return $value["variation_id"];
};
$a2 = array_map($check_var_id, $data);
$x = combine_array($a2);
echo '<pre>'; print_r($x); echo '</pre>'

如果我编译这个脚本,我会得到像这样的结果

Array
(
    [0] => Array
        (
            [0] => S
            [1] => 14 Inches
        )

    [1] => Array
        (
            [0] => S
            [1] => 18 Inches
        )

    [2] => Array
        (
            [0] => M
            [1] => 14 Inches
        )

    [3] => Array
        (
            [0] => M
            [1] => 18 Inches
        )

    [4] => Array
        (
            [0] => 14 Inches
            [1] => S
        )

    [5] => Array
        (
            [0] => 14 Inches
            [1] => M
        )

    [6] => Array
        (
            [0] => 18 Inches
            [1] => S
        )

    [7] => Array
        (
            [0] => 18 Inches
            [1] => M
        )

)

你看,有

[0] => Array
        (
            [0] => S
            [1] => 14 Inches
        )

[4] => Array
        (
            [0] => 14 Inches
            [1] => S
        )

为什么它会再循环.. 有人请帮帮我..

0 个答案:

没有答案