PHP array
PHP Array
Array provides a quick and convenient way to manage a group of related data and is an important part of PHP programming.
Arrays are divided into one-dimensional arrays, two-dimensional arrays and multi-dimensional arrays in php, but whether they are one-dimensional or multi-dimensional, arrays can be uniformly divided into two types: numerical index arrays and associative arrays.
Arrays can store multiple values in a single variable:
Example
<?php $phones=array("Iphone","Nokia","Oppo"); echo "I like " . $phones[0] . ", " . $phones[1] . " and " . $phones[2] . "."; ?>
What is an array?
An array is a special variable that can store multiple values in a single variable.
If you have a list of items (for example: a list of phone names), store it into a single variable like this:
$phones1
="Iphone";
$phones2
="Nokia";
$phones3
="Oppo";
However, what if you want to iterate through the array and find a specific one? What if the array has not just 3 items but 300?
The solution is to create an array!
Arrays can store multiple values in a single variable, and you can access the values within them based on their keys.
Creating Arrays in PHP
In PHP, the array() function is used to create arrays :
array();
In PHP, there are three types of arrays:
· Numeric array - array with numeric ID keys
· - with with a specified key an array, each key associated with a value. #PHP Numeric Array (Index Array)
There are two ways to create a numerical array:
Automatically assign ID keys (ID keys always start from 0):
=array("Iphone","Nokia","Oppo");
Manually assigned ID key:
$phones[0]
= "Iphone";
$phones[1]
="Nokia";
$phones[2]="Oppo";
The following example creates a numerical array named $cars, assigns three elements to the array, and then prints a text containing the array value:
Example
<?php $cars=array("Volvo","BMW","Toyota"); echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; ?>
Get the length of the array - count() function
Example<?php
$cars=array("Volvo","BMW","Toyota");
echo count($cars);
?>
Syntax:
int count (mixed $var [, int $mode = COUNT_NORMAL ] )
echo count($names);
Traverse the numerical array
Traverse and print all values in the numeric array, you can use a for loop, as shown below:Example PHP Associative Arrays Associative arrays are arrays using specified keys that you assign to the array. In addition to array index arrays, PHP also has an associative array, which is generally called hash or map in other computer languages. $info = [ Associative arrays cannot obtain data using numeric subscripts. For example, the value of $info[0] is empty, and we need to use the key as the subscript. The value of $info['age'] is 18. There are two ways to create associative arrays: $age=array("Peter"=>"35"," Ben"=>"37","Joe"=>"43"); or: $age['Peter']="35"; The specified key can then be used in the script: Example Traverse associative array To traverse and print all the values in the associative array, you can use foreach Loop, as shown below: Instance ##Multidimensional array Print array Get array elements We need to understand that an array is not necessarily a simple list of subscripts and values. In fact, each element in the array can also be another array So if the array elements in a one-dimensional array It is an array again, then it becomes a two-dimensional array Dimensions of the array: two-dimensional $arr = [ echo count($arr); How to get "Li Qiang": $arr[1][1] Dimensions of the array : Three-dimensional $arr = [ echo count($arr); How to get "Liu Jun": $arr[1][1][1] ##Complete PHP Array Reference Manual<?php
$cars=array("Volvo","BMW","Toyota");
$arrlength=count($cars);
for($x=0;$x<$arrlength;$x++)
{
echo $cars[$x];
echo "<br>";
}
?>
'name' => 'andy',
'age' => 18,
'gender' => 'male'
] ;
$ age['Ben']="37";
$age['Joe']="43";<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
['Wang Gang', ' Zhang Li', 'Liu Wei'],
['Sun Li', 'Li Qiang', 'Li Guoqing'],
['Zhao Yuanyuan', 'Ding Lili']
];
echo count($arr, true);
[
] ['Wang Gang', 'Zhang Li', 'Liu Wei'],
['Sun Li', 'Li Qiang', 'Li Guoqing '],
['Zhao Yuanyuan', 'Ding Lili']
],
[
] ['Song Hong', 'Ma Xiaoli'],
['Zhang Ying', 'Liu Jun ', 'Huang Tao'],
['Du Lei', 'Zhu Tingting']
],
];
echo count($ arr, true);