Hướng dẫn traverse multidimensional array php

Topic: PHP / MySQLPrev|Next

Nội dung chính

  • Answer: Use the PHP nested loop
  • Related FAQ
  • Actual PHP Output
  • Answer: Using nested loop
  • Example: foreach loop through multi-dimensional array
  • Example: foreach loop through multi-dimensional array
  • How do you use each loop in a multidimensional array?
  • Can we use for loop for array in PHP?
  • What is the syntax of multidimensional array in PHP?
  • How do you print a 2

Answer: Use the PHP nested loop

You can simply use the foreach loop in combination with the for loop to access and retrieve all the keys, elements or values inside a multidimensional array in PHP.

Let's take a look at the following example to understand how it basically works:

Here are some more FAQ related to this topic:

  • How to get single value from an array in PHP
  • How to get all the values from an associative array in PHP
  • How to display array structure and values in PHP

I have an array:

$arr_nav = array[ array[ "id" => "apple", 
          "url" => "apple.html",
          "name" => "My Apple" 
        ],
        array[ "id" => "orange", 
          "url" => "orange/oranges.html",
          "name" => "View All Oranges",
        ],
        array[ "id" => "pear", 
          "url" => "pear.html",
          "name" => "A Pear"
        ]       
 ];

Which I would like to use a foreach loop to replace [which only allows me to set the number:

for [$row = 0; $row < 5; $row++]

with the ability to display a .first and .last class for the relevant array values

Edit

I would like the data to be echoed as:

  • "' . $arr_nav[$row]["name"] . '
  • ' . "\r\n";

    Many thanks for your quick responses. StackOverflow rocks!

    In this article, we show how to loop completely through a multidimensional array in PHP.

    A multidimensional array is an array that contains arrays.

    So let's say we have created the following multidimensional array below composed of people: their name, email, city, and state. The second block of PHP shows how to access all of the data in the array.

    So this is how we can loop through a multidimensional array in PHP.

    The first foreach[] loop obtains each array within the array.

    The entire array is called $people. The $person variable represents each array within the $people array.

    We then use another foreach[] loop. This time we take the individual array within the parent array and we loop through all the key-value pairs of the array. All the key-value pairs represent the name, email, city, and state values of each person.

    We echo out these key-value pairs.

    We create a variable called $num and initially it is set equal to 0. However, in the first foreach[] loop, it is incremented by 1. This is so we can numerically each track of each new record. We echo out this number for each array that the foreach loop encounters in the multidimensional array.

    Because multidimensional arrays are basically arrays nested inside other arrays, all we have to do to loop through them is use nested loops.

    Running the above PHP code, we get the following output below.

    Actual PHP Output

    # 1
    name: Jennifer Kimbers
    email:
    city: Seattle
    state: Washington

    # 2
    name: Rodney Hutchers
    email:
    city: Los Angeles
    state: California

    # 3
    name: Robert Smith
    email:
    city: Michigan
    state: Missouri

    Related Resources

    How to Access the Elements of a Multidimensional Array in PHP

    1. Home
    2. PHP How To
    3. Foreach loop through multidimensional array in PHP

    Answer: Using nested loop

    A multi-dimensional array in PHP is an array consists of one or more arrays. We can access or retrieve the elements of a multi-dimensional array using the foreach loop along with the for loop.

    Example: foreach loop through multi-dimensional array

    In the given example, we have accessed the elements of the multi-dimensional array using for loop along with the foreach loop.

      
     
    
    	Foreach loop through multidimensional array
     
      
    	
      
      

    Output

    We can also access the elements of the multi-dimensional array using the nested foreach loop. Here, we have used the nested foreach loop to iterate through every item in every array inside the array $employees and then print them using the echo.

    Example: foreach loop through multi-dimensional array

    In the given example, we have used the nested foreach loop to iterate through every element within the given multi-dimensional array.

      
     
    
    	Foreach loop through multidimensional array
     
      
    	
      
      

    Output

    Conclusion

    In this lesson, we have learned how to loop through multi-dimensional arrays in PHP. Here we have discussed two methods to access all the array elements of the multi-dimensional array. At first, we have accessed elements of the given array using the for loop along with the foreach loop. After that, we have used the nested foreach loop to access the elements of the given multi-dimensional array.

    How do you use each loop in a multidimensional array?

    For example, write the foreach loop where the $bikes variable is the array. Set the $number variable as key and the $bike variable as value. Next, write another foreach loop inside the just created loop. In the nested loop, write the $bike variable as an array and set the $num and $value as key and value.

    Can we use for loop for array in PHP?

    The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.

    What is the syntax of multidimensional array in PHP?

    PHP multidimensional array is also known as array of arrays. It allows you to store tabular data in an array. PHP multidimensional array can be represented in the form of matrix which is represented by row * column. ... PHP Multidimensional Array Example..

    How do you print a 2

    Code: public class Print2DArrayInJava { public static void main[String[] args] { //below is declaration and intialisation of a 2D array final int[][] matrx = { { 11, 22}, { 41, 52}, }; for [int r = 0; r < matrx. length; r++] { //for loop for row iteration. for [int c = 0; c < matrx[r].

    Chủ Đề