douxi3977 2018-03-30 06:15
浏览 36
已采纳

使用递增键打印多维数组

I have two arrays first one is student name and the second one is his favorite author sekection as follows

$student array is as follows

 array (size=3)
      0 => string 'Jane' (length=4)
      1 => string 'Michelle' (length=8)
      2 => string 'Mark' (length=4)

Now the second array Author $selection is as follows:

array (size=3)
  0 => 
    array (size=3)
      0 => string 'Mark Twaine' (length=11)
      1 => string 'E A Poe' (length=7)
      2 => string 'James Joyce' (length=11)
  1 => 
    array (size=3)
      0 => string 'Oscar' (length=11)
      1 => string 'Leo Toby' (length=7)
      2 => string 'James Joyce' (length=11)
  2 => 
    array (size=3)
      0 => string 'Leo Toby' (length=11)
      1 => string 'E A Poe' (length=7)
      2 => string 'James Joyce' (length=11)

Now I want to display the student name and his favorite authors i.e, Jane favorite authors are Mark Twaine, E A Poe, James Joyce and Michelle favorite authors are Oscar, Leo Toby, James Joyce and Mark favorite authors are Leo Toby, E A Poe, James Joyce ...

I have tried this so far

foreach( $student as $key => $val  ) {
    echo $val." read ";
    foreach( $selection as $key1 ) {
        foreach ($key1 as $key2 => $val2){

        echo $val2;
        echo ' and ';
    } 
       echo "<br/>"; 
}

and getting this as output

Jane favorite is Mark Twaine and E A Poe and James Joyce and 
Oscar and Leo Toby and James Joyce and 
Leo Toby and E A Poe and James Joyce and 
Michelle favorite is Mark Twaine and E A Poe and James Joyce and 
Oscar and Leo Toby and James Joyce and 
Leo Toby and E A Poe and James Joyce and  
Mark favorite is Mark Twaine and E A Poe and James Joyce and 
Oscar and Leo Toby and James Joyce and 
Leo Toby and E A Poe and James Joyce and 

Instead of

    Jane favorite is Mark Twaine and E A Poe and James Joyce
    Michelle favorite is Oscar and Leo Toby  and James Joyce
    Mark favorite is Leo Toby and E A Poe and James Joyce

I want foreach loop to restrict to only one single array value with incrementing key

  • 写回答

2条回答 默认 最新

  • dongzhan5246 2018-03-30 06:23
    关注

    You can use array_combine() to combine students and selection. Then, use implode() to echo the selection per student:

    $student = ['Jane','Michelle','Mark'];
    $selection = [
      ['Mark Twaine', 'E A Poe', 'James Joyce'],
      ['Oscar', 'Leo Toby', 'James Joyce'],
      ['Leo Toby', 'E A Poe', 'James Joyce'],
    ];
    
    $comb = array_combine($student, $selection);
    
    foreach ($comb as $student => $item) {
      echo $student . ' favorite is '. implode(' and ', $item). '<br>' ;
    }
    

    Outputs:

    Jane favorite is Mark Twaine and E A Poe and James Joyce
    Michelle favorite is Oscar and Leo Toby and James Joyce
    Mark favorite is Leo Toby and E A Poe and James Joyce
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题