Array traversing


Traversing an array of values


Traversing all the elements in an array is a common operation that can be done during traversal or other functions.

<1> traverse the array using the for structure;

example

 <?php
/ / Declare an array, the value is 1 to 10
$num = array(1,2,3,4,5,6,7,8,9,10);
 
/ / According to the characteristics of the index array, the subscript starts from 0. So the subscript of 1 is 0, and the subscript of 10 is 9
Echo $num[0].'
';
Echo $num[9].'
';
 
 
//We can get the total number of elements in the array, which is 10
Echo count($num);
 
/ / Traverse the index array, we can define a variable for $i
//$i has a value of 0, starting at 0
//You can set a loop condition as: $i loops within the maximum value of the subscript (9)
For($i = 0 ; $i < count($num) ; $i++){
 
    Echo $num[$i].'
';
 
}
 
?>

You can complete the traversal of the array.

Starting from 0, define $i=0. Add $i to 1 for each iteration, but it must be less than 10 because the maximum value of the array subscript is 9.

In this way, we have learned the traversal of the indexed subscript array.

<2> traversing the array using a foreach structure;

The for loop can traverse the indexed array of consecutive subscripts. However, we found that we can't traverse the associative array, and we can't traverse the index array with subscripts that are not consecutive.

Then we actually learn a loop, there is a Boolean loop is specifically used to loop arrays. The basic syntax of this loop is the basic syntax of foreach.

The syntax is as follows:

Foreach (the array variable to be looped as [key variable =>] value variable) {
/ / Loop structure
}

Traversing associative arrays


 <?php
$data = [
        'fj' => '凤姐',
        'fr' => '芙蓉',
    ];
 
 
Foreach($data as $key => $value){
        Echo $key . '-------' . $value . '
';
}
 
 
//If we only want to read the value, we can delete the following $key =>. When reading, we only read the value. After completing the above experiment, you can open the code below and experiment several times.
 
/*
Foreach($data as $value){
        Echo $value . '
';
}
*/
?>

Traversing the index array

We can traverse successive index arrays through foreach, as in the following example:

 <?php
$data = array(
        0 => 'China',
        100 => 'United States',
        20 => 'Korea',
        300 => 'Germany',
    );
 
//I can do the experiment myself later, loop through the array below.
//$data = array(1,2,3,4,5,6,7,8,9,10);
 
 
Foreach($data as $k => $v){
 
    Echo $k . '------' . $v .'
';
 
}
 
?>

Traversing multidimensional arrays


 <?php
$data = array(
 
        0 => array(
            'China' => 'china',
            'United States' => 'usa',
            'Germany' => 'Germany',
        ),
 
        1 => array(
            '湖北' => 'hubei',
            'Hebei' => 'hebei',
            '山东' => 'shandong',
            '山西' => 'sanxi',
        ),
 
);
 
//Note: When we use a foreach loop, the first loop assigns two arrays with a key of 0 and a key of 1 to a variable ($value). Then, loop through the $value variable, and the value in $value is taken out and assigned to $k and $v.
 
Foreach($data as $value){
 
    //The first loop assigns the array of countries to $value
    //The second loop assigns an array of Chinese provinces to $value again.
    //So, I traverse $value again when I loop
 
    Foreach($value as $k => $v){
            Echo $k . '-----' . $v .'
';
    }
 
    / / In order to see more clearly, I add a gorgeous dividing line in the middle for you to analyze
 
    Echo '---------- split line-----------
';
 
}
 
?>
to sum up:

1. In the first loop, assign the array to $value and then use foreach to loop $value. The key in the two-dimensional subarray is given to $k, and the value is assigned to the variable $v.

2. The first loop exits the loop of the subarray and executes the subsequent code to display the split line.

3. By analogy, the same is true for the second cycle.

Last Updated: 2018-07-06 12:32:44 By akshay

SHARE THIS

share_facebook share_twitter share_google_plus

Please Comment Here :