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):

$phones

=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


count() function is used to return the length of the array (the number of elements Quantity):

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
 $cars=array("Volvo","BMW","Toyota");
 $arrlength=count($cars);
 
 for($x=0;$x<$arrlength;$x++)
 {
 echo $cars[$x];
 echo "<br>";
 }
 ?>

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 = [
'name' => 'andy',
'age' => 18,
'gender' => 'male'
] ;

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";
$ age['Ben']="37";
$age['Joe']="43";

The specified key can then be used in the script:

Example

<?php
 $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
 echo "Peter is " . $age['Peter'] . " years old.";
 ?>


Traverse associative array

To traverse and print all the values ​​in the associative array, you can use foreach Loop, as shown below:

Instance

<?php
 $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
 
 foreach($age as $x=>$x_value)
 {
 echo "Key=" . $x . ", Value=" . $x_value;
 echo "<br>";
 }
 ?>


##Multidimensional array

Multidimensional Arrays will be introduced in detail in the PHP Advanced Tutorial.


Print array

We can use echo to print a string, integer, floating point type, but we cannot use it to print an array

The array is composed of a series of elements. If you want to print, then each element should be printed, not the entire array

We Generally, print_r is used to print arrays (of course, var_dump can also be used, but the structure is not clear)

bool print_r (mixed $expression [, bool $return] )

print_r($names);

When the second parameter is true, print_r will not print the array directly, but will return the printed content as a string.

echo print_r($names, true);

Get array elements

Each element in the index array has a self-increasing serial number. By default, 0 Represents the first element, for

$names = ['andy', 'tom', 'jack'];

We can use the method of adding square brackets after the array variable name to obtain Elements in the array, for example, use $names[0] to get andy, similarly $names[2] represents jack

The dimension of the array: one dimension

$arr ​​= ['Wang Gang', 'Zhang Li', 'Liu Wei'];

Assuming that the array variable is named $arr, the way to obtain "Zhang Li" is: $arr[1]

Multi-dimensional array

Arrays with more than one dimension can be called multi-dimensional arrays

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 ​​= [
['Wang Gang', ' Zhang Li', 'Liu Wei'],
['Sun Li', 'Li Qiang', 'Li Guoqing'],
['Zhao Yuanyuan', 'Ding Lili']
];

echo count($arr);
echo count($arr, true);

How to get "Li Qiang": $arr[1][1]

Dimensions of the array : Three-dimensional

$arr ​​= [
[
] ['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);
echo count($ arr, true);

How to get "Liu Jun": $arr[1][1][1]

##Complete PHP Array Reference Manual

For a complete reference manual for all array functions, please visit our PHP Array Reference Manual.

This reference manual provides a brief description and application examples of each function!


Continuing Learning
||
<?php $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); foreach($age as $x=>$x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "<br>"; } ?>
submitReset Code