在表中打印多维数组、键和值

我有以下数组


$consulta2 = Array ( 

                    [AB] => Array ( [0] => Noah [1] => 48 ) 

                    [BC] => Array ( [0] => Jacob [1] => 42 ) 

                    [DA] => Array ( [0] => Mason [1] => 27 )

                   )

我想在表中打印带有 KEYS 和 VALUES 的数组


我目前正在使用这个 foreach 循环


foreach ($consulta2 as $key => $value){

        foreach ($value as $k => $v) {

          $template.= '<tr>

          <td class="service">'.$key.'</td>

          <td class="desc">'.$v.'</td> \r \n

          <td class="service">'.$v.'</td>

          <td class="service"></td>

          </tr>';

        }

    }

我得到下表


+-----+-------+-------+

| Key | Value | Value |

+-----+-------+-------+

|  AB |  Noah |  Noah |

+-----+-------+-------+

|  AB |   48  |   48  |

+-----+-------+-------+

|  BC | Jacob | Jacob |

+-----+-------+-------+

|  BC |   42  |   42  |

+-----+-------+-------+

|  DA | Mason | Mason |

+-----+-------+-------+

|  DA |   27  |   27  |

+-----+-------+-------+

但我希望它看起来像这样


+-----+-------+-------+

| Key | Value | Value |

+-----+-------+-------+

|  AB |  Noah |   48  |

+-----+-------+-------+

|  BC | Jacob |   42  |

+-----+-------+-------+

|  DA | Mason |   27  |

+-----+-------+-------+

如果有人知道如何解决它,我将非常感激


小怪兽爱吃肉
浏览 91回答 1
1回答

qq_笑_17

$value删除内循环并通过键 (0和)访问项目1:foreach ($consulta2 as $key => $value){&nbsp; &nbsp; $template.= '<tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="service">'.$key.'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="desc">'.$value[0].'</td> \r \n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="service">'.$value[1].'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="service"></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>';}
打开App,查看更多内容
随时随地看视频慕课网APP