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
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
The problem is that your second foreach()
(foreach( $selection as $key1 ) {
) is looping over all of the selections for every student. At this point you need to just pick out the selection for the corresponding student (matching the key of the $student array to the one of the $selection array).
$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/>";
}
You can see the inner foreach
uses the $key
from the first array to pick out which selection to loop over.
You can shorten the inner loop using implode()
, and this also gets rid of the extra 'and' in the output.
foreach( $student as $key => $val ) {
echo $val." read ".implode(' and ', $selection[$key])."<br/>";
}