猿问

如何展平和输出两个多维数组并打印成对值的组合?

我有 2 个输入数组:


$data1 = array (

    0 => 0,

    1 => array ( 0 => 4, 1 => 8, ),

    2 => 0,

    3 => array ( 0 => 2, 1 => 6, 2 => 10, ),

    4 => array ( 0 => 1, 1 => 5, ),

    5 => array ( 0 => 3, 1 => 7, 2 => 11, ),

)


$data2 = array (

    0 => 0,

    1 => array ( 0 => 1, 1 => 5, ),

    2 => array ( 0 => 4, 1 => 8, ),

    3 => 0,

)

从第一个数组读取叶节点时,我的意思是迭代第二个数组的所有叶节点并打印所有配对组合。


我的问题是,只有子数组中的最后一个值$data1被打印,并且只有最后一个值$data2被打印。


我的代码:


foreach ($data1 as $sourcevalue){

    if( is_array($sourcevalue) == "1")

    {

        foreach ($sourcevalue as $value)

        {

            $source=$value;

        }

    }

    else 

    {

        $source=$sourcevalue;

    }


    foreach ($data2 as $endvalue){

        if( is_array($endvalue) == "1")

        {

            foreach ($endvalue as $value1){

                $end=$value1;

            }

        }

        else 

        {

            $end=$endvalue;

        }

    }

    echo $source." ".$end."<br>";   

}

我目前的输出:


0 0

8 0

0 0

10 0

5 0

11 0

期望的输出:


0 0

0 1

0 5

0 4

0 8

0 0

4 0

4 1

4 5

4 4

4 8

4 0

8 0

8 1

8 5

8 4

8 8

8 0

0 0

0 1

0 5

0 4

0 8

0 0

2 0

2 1

2 5

2 4

2 8

2 0

6 0

6 1

6 5

6 4

6 8

6 0

10 0

10 1

10 5

10 4

10 8

10 0

1 0

1 1

1 5

1 4

1 8

1 0

5 0

5 1

5 5

5 4

5 8

5 0

3 0

3 1

3 5

3 4

3 8

3 0

7 0

7 1

7 5

7 4

7 8

7 0

11 0

11 1

11 5

11 4

11 8

11 0

我如何修改我的代码以显示所有组合?


LEATH
浏览 92回答 2
2回答

慕妹3242003

您也需要回显中间元素,而不仅仅是最后一个foreach ($data1 as $sourcevalue){&nbsp; &nbsp; if (is_array($sourcevalue)) {&nbsp; &nbsp; &nbsp; &nbsp; foreach ($sourcevalue as $value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printData2($value);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; printData2($sourcevalue);&nbsp; &nbsp; }}function printData2($source) {&nbsp; &nbsp; foreach ($data2 as $endvalue){&nbsp; &nbsp; &nbsp; &nbsp; if (is_array($endvalue)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($endvalue as $value1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pr($source, $value1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pr($source, $endvalue);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}function pr($source, $end) {&nbsp; &nbsp; echo "{$source} {$end}<br>";}

江户川乱折腾

数组 2 中的数据在处理过程中永远不会改变,因此最好准备/展平数据以避免is_array()在迭代嵌套循环时进行冗余调用。这样做的一种方法是使用array_merge_recursive()仅访问叶节点(不可迭代)并生成一维数组 ( $flat2)。另一个好技巧是将元素(无论它们是否可迭代)转换为数组,而不是在is_array()迭代时进行调用。$data1如果将不可迭代元素 ( $e) 转换为数组,它将成为新数组中的唯一值。换句话说,$e变成了[$e]。这两个步骤对清理代码大有帮助。在我下面的代码片段中,使用两个 foreach 循环遍历$data1,然后使用第三个循环迭代$flat2,最后使用printf()干净地格式化和输出数据。$data1请注意,我在 Justinas 的回答下根据您的要求包括了第一级密钥。代码:(演示)$flat2 = [];array_walk_recursive($data2, function($v) use(&$flat2) {&nbsp; &nbsp; $flat2[] = $v;});foreach ($data1 as $k => $e) {&nbsp; &nbsp; foreach ((array)$e as $v) {&nbsp; &nbsp; &nbsp; &nbsp; foreach ($flat2 as $f) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("%d: %d %d\n", $k, $v, $f);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}或者仅使用语言构造,您可以对两个数组使用相同的数组转换技术,并将第二组嵌套循环嵌套在第一组嵌套循环中。这样会生成更少的变量,少调用1次函数,更简洁。代码:(演示)foreach ($data1 as $k => $e1) {&nbsp; &nbsp; foreach ((array)$e1 as $v1) {&nbsp; &nbsp; &nbsp; &nbsp; foreach ($data2 as $e2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ((array)$e2 as $v2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("%d: %d %d\n", $k, $v1, $v2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}输出:0: 0 00: 0 10: 0 50: 0 40: 0 80: 0 01: 4 01: 4 11: 4 51: 4 41: 4 81: 4 01: 8 01: 8 11: 8 51: 8 41: 8 81: 8 02: 0 02: 0 12: 0 52: 0 42: 0 82: 0 03: 2 03: 2 13: 2 53: 2 43: 2 83: 2 03: 6 03: 6 13: 6 53: 6 43: 6 83: 6 03: 10 03: 10 13: 10 53: 10 43: 10 83: 10 04: 1 04: 1 14: 1 54: 1 44: 1 84: 1 04: 5 04: 5 14: 5 54: 5 44: 5 84: 5 05: 3 05: 3 15: 3 55: 3 45: 3 85: 3 05: 7 05: 7 15: 7 55: 7 45: 7 85: 7 05: 11 05: 11 15: 11 55: 11 45: 11 85: 11 0
随时随地看视频慕课网APP
我要回答