Php array within array loops


(This question is not related to any of the courses in the catalog, however, the answer might be in one of them, but I am looking for an answer for a unique situation. Any links to any lessons, or a direct answer will help greatly. Thank you.)

Think of this, I have two arrays, one array under the name of $arrOne contains 5 arrays all named $arrTwo, $arrThree, (I’m sure you get the point now). Each array in $arrOne has 5 elements each. How can I write a loop to echo each 4th element in each array inside of $arrOne?


To use a for loop with an array, we can use foreach:

foreach ($myArray as $item) {
   /* do something with $item */

In order to access a particular item from an array, we can use its index:

$myValue = $myArray[0] /* get the first item from $myArray */

These can therefore be combined:

/* Loop through each array in $myArray and store its first item */
foreach ($myArray as $anotherArray) {
   $myValue = $anotherArray[0];

Hope this helps!