Array (Array) in PHP
An array is a data structure that stores one or more of the same value types in a single value.
An array is a data structure that stores one or more of the same value types in a single value. For example, if you want to store 100 numbers, then instead of defining 100 variables, it is easy to define an array of length 100.
There are 3 different array types and each array value is accessed using an ID, which is called an array index.
- Integer array - An array with indexes in the form of numbers. Values are stored and accessed linearly.
- Combined array - An array with an index in the form of a string. This array stores element values by combining key values instead of in a strict linear index order such as an integer array.
- Multidimensional array - An array containing one or more arrays and values accessed using multiple indexes.
Note - Available array handling functions are provided in the Array handling function in PHP
Array of integers in PHP
Arrays of this type can store numbers, strings, and any object, but the array index is still represented by numbers. By default, the array index starts at 0.
For example
This array can store numbers, strings and any objects, but their indexes will be represented in numeric form. By default the array index starts at 0.
Below is an example to illustrate how to create and access integer arrays in PHP.
Here, we used the array () function to create the array. This function is explained in the sub-chapter: The array function in PHP above.
";
}
/ * The second method for creating arrays in PHP. * /
$ numbers [0] = "one";
$ numbers [1] = "two";
$ numbers [2] = "three";
$ numbers [3] = "four";
$ numbers [4] = "five";
foreach ($ numbers as $ value)
{
echo "The array element value is $ value
";
}
?>
Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:
Combined array in PHP
Complex arrays are quite similar to feature arrays, but they are different in terms of indexes. The federated array will have a string index so that you can set a strong link between the key and the value .
To store employee salaries in an array, an indexed array will not be the best option. Instead, we will use employee names as keys in the federated array, and value will be their corresponding pay.
Note : Do not keep the conjugate array inside double quotation marks while printing, otherwise it will not return any value.
For example
";
echo "The salary of strong staff is". $ luong_nhan_vien ['manh']. "
";
echo "The salary of the employee is". $ luong_nhan_vien ['huong']. "
";
/ * The second method for creating conjugate arrays. * /
$ luong_nhan_vien ['wild'] = "high";
$ luong_nhan_vien ['manh'] = "medium";
$ luong_nhan_vien ['huong'] = "low";
echo "Wild employee salary is". $ luong_nhan_vien ['wild']. "
";
echo "The salary of strong employees is". $ luong_nhan_vien ['manh']. "
";
echo "Employee salary is". $ luong_nhan_vien ['huong']. "
";
?>
Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:
Multidimensional array in PHP
In a multidimensional array, each element can also be an array. And each element in an extra array can be an array, and so on. Values in the diverse array are accessed using multiple indexes.
For example
In this example, we create a two-dimensional array to store the 3 subject points of 3 students.
This example is a multidimensional array, you can create an integer array in the same way.
php $diemThi = array ( "hoang" => array ( "monVatLy" => 7 , "monToan" => 8 , "monHoa" => 9 ), "manh" => array ( "monVatLy" => 7 , "monToan" => 9 , "monHoa" => 6 ), "huong" => array ( "monVatLy" => 8 , "monToan" => 8 , "monHoa" => 9 ) ); /* truy cập các giá trị của mảng đa chiều */ echo "Điểm thi môn Vật Lý của hoang là: " ; echo $diemThi [ 'hoang' ][ 'monVatLy' ] . "
" ; echo "Điểm thi môn Toán của manh là: " ; echo $diemThi [ 'manh' ][ 'monToan' ] . "
" ; echo "Điểm thi môn Hóa của huong là: " ; echo $diemThi [ 'huong' ][ 'monHoa' ] . "
" ; ?>
Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:
Follow tutorialspoint
Previous article: Loop in PHP
Next lesson: String (String) in PHP
You should read it
- Cursor and Array in C ++
- Array data structure
- Array in Python
- Arrays and objects in JavaScript are like stories and newspapers!
- Array in Language C
- How to Copy and Create Arrays in SketchUp
- Variables are predefined in PHP
- How to Print an Array in Java
- Array of pointers in C ++
- string.h in C
- How to Use I/O Streams in Java
- Overload subscript operator [] in C ++
Maybe you are interested
Set of beautiful 4k quality nature wallpapers for computers Wallpaper of the Lunar New Year 2018 for beautiful phones Download now a set of lovely Tet cover photos for Facebook Guide interactive statistics Reactions when Livestream on Facebook Instructions for adding personal information on TalkTV Guide to join chat rooms on TalkTV (ccTalk)