【发布时间】: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