Some methods of php array definition array 1. $ apparray (a, B, c,); echo $ app [0]; output a2. $ aaarray (& nbsp; & nbsp; & nbs php array methods
Define an array
1.
$ App = array (
'A ',
'B ',
'C ',
);
Echo $ app [0];
Output
A
2.
$ Aa = array (
'Title' => 'generic error ',
'Body' => 'BB ',
'Type' => 'error ');
Echo $ aa ['title'];
Output
Generic Error
Is_array ()
Determines whether a variable is an array.
Array_push (,)
Add data to an array
Foreach traversal array
$ Arr_a = array (
'A ',
'B ',
'C'
);
Foreach ($ arr_a as $ ){
Echo $ a. "\ n ";
}
Output
A
B
C
$ Arr_ B = array (
'A' => 'A ',
'B' => 'BB ',
'C' => 'CC'
);
Foreach ($ arr_ B as $ ){
Echo $ a. "\ n ";
}
Output
Aa
Bb
Cc
List function
The list function assigns values in the array to some variables. The syntax is as follows:
Void list (mixed $ varname, mixed $ varname ...)
Like array (), this is not a real function, but a language structure. List () assigns values to a group of variables with one-step operations.
Note: list () can only be used as an array of numeric indexes and assumes that the numeric index starts from 0.
Example 1. list ()
Code:
$ Arr = array (1, 2, 3 );
List ($ a, $ B, $ c) = $ arr;
Echo "\ $ a is $,
\ $ B is $ B,
\ $ C is $ c.
";
?>
Shown:
$ A is 1,
$ B is 2,
$ C is 3.
The each function returns the current key/value pair in the array and moves the array pointer one step forward. Note that it is a pair, which is described in detail below.
Syntax of this function:
Array each (array & $ array)
Returns the key/value pair of the current pointer position in the array and moves the array pointer forward. The key-value pair is returned as an array of four units.
The values are 0, 1, key, and value. Unit 0 and key contain the key name of the array unit, and 1 and value contain data. If
If the pointer crosses the end of the array, each () returns FALSE. Why are there four tables in the each function? In fact, the each letter
We can use 0, 1 as the index, or key and value as the index.
Example 2: each
Code:
$ Arr = array ("I am the first value", "I am the second value", "I am the third value ");
Echo "when we use 0, 1 as the index:
";
$ A = each ($ arr );
Echo "my position in the \ $ arr array is:". $ a ['0'];
Echo"
";
Echo "my value in the \ $ arr array is:". $ a ['1'];
Echo"
";
Echo "when key and value are used as indexes:
";
$ B = each ($ arr );
Echo "my position in the \ $ arr array is:". $ B ['key'];
Echo"
";
Echo "my value in the \ $ arr array is:". $ B ['value'];
?>
Shown:
When we use 0, 1 as the index:
My position in the $ arr array is: 0
My value in the $ arr array is: I am the first value.
When key and value are used as indexes:
My position in the $ arr array is: 1
My value in the $ arr array is: I am the second value.
Reset (). If this is the first time you traverse the array, you do not need to use it.
For details, refer to the reset function:
Reset is to point the internal pointer of the array to the first unit. syntax:
Mixed reset (array & $ array)
Reset () returns the internal pointer of array to the first unit and the value of the first array unit. If the array is null, FALSE is returned.
You can compare the above example 2 with the following example to understand it easily... ,
Code:
$ Arr = array ("I am the first value", "I am the second value", "I am the third value ");
Echo "when we use 0, 1 as the index:
";
$ A = each ($ arr );
Echo "my position in the \ $ arr array is:". $ a ['0'];
Echo"
";
Echo "my value in the \ $ arr array is:". $ a ['1'];
Echo"
";
Echo "when key and value are used as indexes:
";
Reset ($ arr );
$ B = each ($ arr );
Echo "my position in the \ $ arr array is:". $ B ['key'];
Echo"
";
Echo "my value in the \ $ arr array is:". $ B ['value'];
?>
Shown:
When we use 0, 1 as the index:
My position in the $ arr array is: 0
My value in the $ arr array is: I am the first value.
When key and value are used as indexes:
My position in the $ arr array is: 0
My value in the $ arr array is: I am the first value. // note that this line in Example 2 is displayed.
Array traversal using list and each
$ Arr = array ('a' => 'red', 'B' => 'white', 'C' => 'blue ');
Reset ($ arr );
While (list ($ key, $ val) = each ($ arr )){
Echo "arr [". $ key. "] =". $ val;
}