【问题标题】:Print Multidimensional array with incrementing key打印带递增键的多维数组
【发布时间】:2018-03-30 06:15:45
【问题描述】:

我有两个数组第一个是学生姓名,第二个是他最喜欢的作者选择如下

$student 数组如下

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

现在第二个数组Author $selection如下:

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)

现在我想显示学生姓名和他最喜欢的作者,即 Jane 最喜欢的作者是 Mark Twaine、E A Poe、James Joyce 和 Michelle 最喜欢的作者是 Oscar、Leo Toby、James Joyce 和 Mark 最喜欢的作者是 Leo Toby、E A坡,詹姆斯·乔伊斯 ...

到目前为止我已经尝试过了

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

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

并将其作为输出

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 

代替

    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

我希望 foreach 循环限制为只有一个带有递增键的数组值

【问题讨论】:

    标签: php multidimensional-array


    【解决方案1】:

    您可以使用array_combine() 将学生和选择结合起来。然后,使用implode() 回应每个学生的选择:

    $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>' ;
    }
    

    输出:

    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
    

    【讨论】:

      【解决方案2】:

      问题是您的第二个foreach() (foreach( $selection as $key1 ) {) 正在循环遍历每个学生的所有选择。此时,您只需为对应的学生挑选出所选内容(将 $student 数组的键与 $selection 数组的键匹配)。

      $student = ['Jane', 'Michelle', 'Mark'];
      $selection = [['Mark Twaine','E A Poe','James Joyce'],
          ['Oscar','Leo Toby','James Joyce'],
          ['Leo Toby','E A Poe','James Joyce']];
      
      foreach( $student as $key => $val  ) {
          echo $val." read ";
          foreach( $selection[$key] as $val2 ) {
                  echo $val2;
                  echo ' and ';
          }
          echo "<br/>";
      }
      

      您可以看到内部foreach 使用第一个数组中的$key 来挑选要循环的选择。

      您可以使用implode() 缩短内部循环,这也消除了输出中多余的“和”。

      foreach( $student as $key => $val  ) {
          echo $val." read ".implode(' and ', $selection[$key])."<br/>";
      }
      

      【讨论】:

        猜你喜欢
        • 2016-12-24
        • 1970-01-01
        • 2012-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多