Javascript 多维数组概率

Javascript 多维数组概率,javascript,php,arrays,multidimensional-array,Javascript,Php,Arrays,Multidimensional Array,您好,我已经处理这个问题一段时间了,似乎无法分析如何使用PHP或javascript输出数组概率。如果有人能够解决这个问题,请提前感谢 例如: $arrayNms = [ ["A", "B", "C"], ["Q", "P"], ["CC", "C3"] ]; /* OUTPUT A, Q, CC A, Q, C3 A, P, CC A, P, C3 B, Q, CC B, Q, C3 B, P, CC B, P, C3 C, Q, CC C, Q, C3 C, P,

您好,我已经处理这个问题一段时间了,似乎无法分析如何使用PHP或javascript输出数组概率。如果有人能够解决这个问题,请提前感谢

例如:

$arrayNms = [
    ["A", "B", "C"],
    ["Q", "P"],
    ["CC", "C3"]
];


/*
OUTPUT
A, Q, CC
A, Q, C3
A, P, CC
A, P, C3
B, Q, CC
B, Q, C3
B, P, CC
B, P, C3
C, Q, CC
C, Q, C3
C, P, CC
C, P, C3
*/

//this is what I got so far, but can't seem to output the desired values
    $arr1 = [];
    for ($i = count($arrayNms) - 1; $i >= 0; $i--) {
        for ($j=0; $j < count($arrayNms[$i]); $j++) { 
            $prdName1 = $arrayNms[$i][$j];
            if(array_key_exists(($i+1), $arrayNms)){
                for ($k=0; $k < count($arrayNms[$i+1]); $k++) { 
                    $prdName2 = $arrayNms[$i][$k];
                    print_r($prdName2.', '.$prdName1);
                }
            }
        }
    }

非常感谢

这看起来像是大多数教科书给学生的学习递归函数的挑战。这样的操作将提供所需的输出,并且无论$arrayNms中有多少个值数组,只要至少有一个:

function print_values($array, $index = 0, $base = "") {
    // check if there's another array of values after this one
    $is_last = !isset($array[$index + 1]);

    // loop through all values in the given sub-array
    foreach ($array[$index] as $value) {
        if ($is_last) {
            // if this is the last array of values, output the current value
            echo $base . $value . PHP_EOL;
        } else {
            // otherwise, append this value to the base and process the next array
            print_values($array, $index + 1, $base . $value . ", ");
        }
    }
}

$arrayNms = [
    ["A", "B", "C"],
    ["Q", "P"],
    ["CC", "C3"]
];

print_values($arrayNms);
输出:

A, Q, CC
A, Q, C3
A, P, CC
A, P, C3
B, Q, CC
B, Q, C3
B, P, CC
B, P, C3
C, Q, CC
C, Q, C3
C, P, CC
C, P, C3
A, Q, CC
A, Q, C3
A, P, CC
A, P, C3
B, Q, CC
B, Q, C3
B, P, CC
B, P, C3
C, Q, CC
C, Q, C3
C, P, CC
C, P, C3

这看起来像是大多数教科书给学生提供的学习递归函数的挑战。这样的操作将提供所需的输出,并且无论$arrayNms中有多少个值数组,只要至少有一个:

function print_values($array, $index = 0, $base = "") {
    // check if there's another array of values after this one
    $is_last = !isset($array[$index + 1]);

    // loop through all values in the given sub-array
    foreach ($array[$index] as $value) {
        if ($is_last) {
            // if this is the last array of values, output the current value
            echo $base . $value . PHP_EOL;
        } else {
            // otherwise, append this value to the base and process the next array
            print_values($array, $index + 1, $base . $value . ", ");
        }
    }
}

$arrayNms = [
    ["A", "B", "C"],
    ["Q", "P"],
    ["CC", "C3"]
];

print_values($arrayNms);
输出:

A, Q, CC
A, Q, C3
A, P, CC
A, P, C3
B, Q, CC
B, Q, C3
B, P, CC
B, P, C3
C, Q, CC
C, Q, C3
C, P, CC
C, P, C3
A, Q, CC
A, Q, C3
A, P, CC
A, P, C3
B, Q, CC
B, Q, C3
B, P, CC
B, P, C3
C, Q, CC
C, Q, C3
C, P, CC
C, P, C3
在爪哇

public class MyClass {
    public static void main(String args[]) {
        String[][] arr = {{"A","B","C"},{"P","Q"},{"CC","C3"}};
        print(arr, 0);
    }


    public static void  print(String[][] arr, int depth)
    {
        if(depth == arr.length)
        {
            for(int i =0; i < arr.length - 1 ; i++)
            {
                System.out.print(arr[i][0] + ",");
            }
            System.out.print(arr[arr.length - 1][0]);
            System.out.println("\r");
        }
        else
        {
            for(int j =0; j < arr[depth].length ; j++)
            {
                String temp = arr[depth][j];
                arr[depth][j] = arr[depth][0];
                arr[depth][0] = temp;
                print(arr, depth + 1);
            }
        }
    }
}
在爪哇

public class MyClass {
    public static void main(String args[]) {
        String[][] arr = {{"A","B","C"},{"P","Q"},{"CC","C3"}};
        print(arr, 0);
    }


    public static void  print(String[][] arr, int depth)
    {
        if(depth == arr.length)
        {
            for(int i =0; i < arr.length - 1 ; i++)
            {
                System.out.print(arr[i][0] + ",");
            }
            System.out.print(arr[arr.length - 1][0]);
            System.out.println("\r");
        }
        else
        {
            for(int j =0; j < arr[depth].length ; j++)
            {
                String temp = arr[depth][j];
                arr[depth][j] = arr[depth][0];
                arr[depth][0] = temp;
                print(arr, depth + 1);
            }
        }
    }
}
在JavaScript中:

var myArr = [
    ["A", "B", "C"],
    ["Q", "P"],
    ["CC", "C3"]
];

for (var el1 in myArr[0]) {
    for (var el2 in myArr[1]) {
        for (var el3 in myArr[2]) {
            console.log(myArr[0][el1]+', '+myArr[1][el2]+', '+myArr[2][el3]);
        }    
    }  
}




/*
A, Q, CC
A, Q, C3
A, P, CC
A, P, C3
B, Q, CC
B, Q, C3
B, P, CC
B, P, C3
C, Q, CC
C, Q, C3
C, P, CC
C, P, C3
*/
var myArr=[ [A,B,C], [Q,P], [抄送,C3] ]; 对于myArr中的变量el1[0]{ 对于myArr中的var el2[1]{ 对于myArr中的var el3[2]{ console.logmyArr[0][el1]+'、'+myArr[1][el2]+'、'+myArr[2][el3]; } } } 在JavaScript中:

var myArr = [
    ["A", "B", "C"],
    ["Q", "P"],
    ["CC", "C3"]
];

for (var el1 in myArr[0]) {
    for (var el2 in myArr[1]) {
        for (var el3 in myArr[2]) {
            console.log(myArr[0][el1]+', '+myArr[1][el2]+', '+myArr[2][el3]);
        }    
    }  
}




/*
A, Q, CC
A, Q, C3
A, P, CC
A, P, C3
B, Q, CC
B, Q, C3
B, P, CC
B, P, C3
C, Q, CC
C, Q, C3
C, P, CC
C, P, C3
*/
var myArr=[ [A,B,C], [Q,P], [抄送,C3] ]; 对于myArr中的变量el1[0]{ 对于myArr中的var el2[1]{ 对于myArr中的var el3[2]{ console.logmyArr[0][el1]+'、'+myArr[1][el2]+'、'+myArr[2][el3]; } } } 类似于:

类似于:


我们可以更改$arrayNms以获得所需的输出吗?$arrayNms来自输入字段,可以提供任何信息吗?我们可以更改$arrayNms以获得所需的输出吗?$arrayNms来自输入字段,可以提供任何信息这是Java,OP要求提供JavaScript。我猜,OP被困在算法上而不是编码上。这是Java,OP要求JavaScript。我猜,OP被困在算法上而不是编码上。