Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:无Nead两次数组循环_Php_Arrays_Loops_Foreach - Fatal编程技术网

PHP:无Nead两次数组循环

PHP:无Nead两次数组循环,php,arrays,loops,foreach,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

你好,我有一个函数来构建变体,但是循环太多了

这就是函数

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>'
你看,有

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

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

首先,请解释combine_数组函数的作用。你到底想用这个脚本实现什么?我想得到这样的结果:数组[0]=>Array[0]=>S[1]=>14英寸[1]=>Array[0]=>S[1]=>18英寸[2]=>Array[0]=>M[1]=>14英寸[3]=>阵列[0]=>米[1]=>18英寸你能帮我一下吗..?我会再问。你到底想用这个函数实现什么?为什么需要三级嵌套foreach?如果你能解释你想做什么,也许有人会建议一个更好的解决方案。因为我的数组有三个级别,也许你有更好的解决方案让我得到我之前展示给你的结果?
[0] => Array
        (
            [0] => S
            [1] => 14 Inches
        )
[4] => Array
        (
            [0] => 14 Inches
            [1] => S
        )