PHP entry level (01) -- Array
Recently, I got a navigation website from my friend and found that it was basically developed using PHP, So I first came into contact with the PHP environment. At the beginning, it was confused. net, so installing a Vs will be all right. For example, Apache, MySQL, and PHP have never been used to it. Later, we installed Wamp and PHP eclipse to start the PHP learning journey. With the. NET Foundation, it seems that it is not too difficult to learn PHP again. However, I still feel a little weird in some places, such as adding a knife before the variable -- $. Therefore, we decided to record the scattered differences.
1.Array Definition
An array in PHP is a set of keywords or values. The array definition uses the keyword array. There are two defining methods:
- Array (value1, value2, value3 ...); This method saves the default key value, and the system automatically starts from 0.
- Array (Key => value, key2 => value2 ...);
The following is a small demo. print_r ($ ARR) is a built-in function used to output the array content.
Note:: PhP variables are of weak type, so they can be different types of values in the array, as shown in.
Code:
Result:
We use PHP system functions.Print _R ($ ARR ),To directly output all the contents of the array.
2.Array content reference and modification:
If it is defined in the form of key => value, array [Key] is used for reference;
If the key value is not directly defined, it is referenced as starting from 0 by default.
Code:
Result:
3.Multi-dimensional array
The PHP array value can be an array, that is, a multi-dimensional array.
Code:
Result:
4.Common array Functions
- Count ($ ARR): calculates the number of array values.
- Is_array ($ ARR): determines whether the variable is an array.
- Explode(Key, value): divides a variable into an array. Value is a regular variable to be split, such as a string. Key is the variable used as the basis for segmentation. It can be a single character or multiple characters.
Code:
Result:
5. Foreach
Syntax: foreach ($ arr as $ key => $ value ){... ...};
This is a slightly important method, which is very different from the. NET syntax. $ Arr is the input array variable. The following is a small example to help you understand it.
Code:
The first day of PHP's learning experience: the syntax is too casual, so I cannot touch it freely!