Array (Array) in PHP

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.

  1. Integer array - An array with indexes in the form of numbers. Values ​​are stored and accessed linearly.
  2. 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.
  3. 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:

Array (Array) in PHP Picture 1

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:

Array (Array) in PHP Picture 2

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:

Array (Array) in PHP Picture 3

Follow tutorialspoint

Previous article: Loop in PHP

Next lesson: String (String) in PHP

4 ★ | 1 Vote

May be interested

  • Top 10 best array games for PCTop 10 best array games for PC
    top 10 best array games for pc you prefer to direct and command soldiers to occupy new bases and lands. simulate epic battles in history through strategy games, arrayed games. let's tipsmake.com find the top
  • Array of pointers in C ++Array of pointers in C ++
    before we understand the concept of pointer arrays, we consider the following example, which uses an array of 3 integer numbers.
  • How to Print an Array in JavaHow to Print an Array in Java
    if you are working on java and have an array with a large amount of data, you may want to print certain elements in order to view them conveniently. there are multiple ways you can print arrays in java and the examples given below will...
  • Indexer in C #Indexer in C #
    indexer in c # helps index objects, such as an array. when you define an indexer for a class, this class operates similarly to a virtual array. then you can access the instance of this class with the array access operator in c # ([]).
  • The Match function (the function searches for a specified value in an array or cell range) in ExcelThe Match function (the function searches for a specified value in an array or cell range) in Excel
    the match function helps you search for a specified value in an array, a range of cells, and then returns the position of the value in that array or range.
  • Use AI and the largest telescope to find aliens and interstellar lifeUse AI and the largest telescope to find aliens and interstellar life
    indian astronomers will play a key role in the 16-nation square kilometer array observatory (skao), whose dish antenna array began setting up this month and will scan the corners. distant universe in 2027.
  • MODE.MULT function - The function returns a vertical array of the most common values ​​in ExcelMODE.MULT function - The function returns a vertical array of the most common values ​​in Excel
    mode.mult function: the function returns a vertical array of the most frequently occurring values, or repeating values ​​in an array or data range. support functions from excel 2010 onwards. syntax: mode.mult ((number1, [number2], ...)
  • Advanced index operation in MongoDBAdvanced index operation in MongoDB
    create an index on the array ie create separate indexes for each of its fields. so in this situation, when we create an index on the array of tags, individual indexes will be created for their values: music, cricket and blogs.
  • Array data structureArray data structure
    array (array) is one of the oldest and most important data structures. arrays can store some fixed elements and these elements have the same type. most data structures use arrays to implement algorithms. here are the important concepts related to arrays.
  • Use array in ShellUse array in Shell
    a shell variable is able to hold a single value. this type of variables is called scalar variables.