PHP array

Arrays can store multiple values ​​in a single variable:

Example

<?php
$cars=array("Volvo","BMW","Toyota");
echo "I like" . $cars[0] . "," . $cars[1] . "and " . $cars[2] . ".";
?>

Try it »


Array What is it?

An array is a special variable that can store multiple values ​​in a single variable.

Arrays provide a quick and convenient way to manage a group of related data and are an important part of PHP programming.

If you have a list of items (for example: a list of car names), store it into a single variable like this:

$cars1 = "Volvo";

$cars2 = "BMW";

$cars3 = "Toyota";

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() ;

After PHP 5.4, array initialization can be used in a more concise way: [] is represented by a pair of square brackets.

In PHP, there are three types of arrays:

1. Numeric array - array with numeric ID keys

2. Associative array - with specified keys An array, each key is associated with a value

3. Multidimensional array - an array containing one or more arrays

PHP Numeric Array

There are two ways to create a numerical array Method:

Automatically assign ID keys (ID keys always start from 0):

$cars=array("Volvo","BMW","Toyota");

Manually assigned ID key:

$cars1[0] = "Volvo";

$cars2[1] = "BMW";

$cars3[2 ] = "Toyota";

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");
var_dump($cars); // 可以打印出来看看是什么 
echo "I like" . $cars[0] . "," . $cars[1] . "and " . $cars[2] . ".";
?>

Try it»


Get the length of the array - count() function

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

Example

<?php
$cars=array("Volvo","BMW","Toyota");
echo count($cars);
?>

Try it »


Traverse Numeric array

Traverse and print all the 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>";
}
?>

Try it »

The for loop will be explained in detail in the following chapters, refer to PHP for loop


PHP associative array

An associative array is an array using specified keys that you assign to the array.

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.";
?>

try it"

Then let’s declare the associative array. The subscript of the string must be specified and the key-value correspondent must be used.

<?php
 //声明一下关联数组
    $rela = array(
              '帅' => '你',
              '很帅' => '黄晓明',
              '灰常灰常帅' => '宁泽涛',
              '有男人味的大叔' => '吴秀波',
             );
 //简洁声明
     $drink = [
            '美' => '凤姐',
            '很美' => '芙蓉姐姐',
            'verymei' => '杨幂',
            '心中滴女神呀' => '华妃',
            100 => '孙俪',
            '娘娘',
           ];
 // 输出 $rela
  echo '<pre>';
  var_dump($rela);
  echo '</pre>';
 // 输出$drink
  echo '<pre>';
  var_dump($drink);
  echo '</pre>';
?>

Let’s experiment and see what the final result is:

array(4)
{
["handsome "]=> string(3) "You"
["Very handsome"]=> string(9) "Huang Xiaoming"
["Grey, often gray, often handsome"]=> string(9) "Ning Zetao"
["Manly uncle"]=> string(9) "Wu Xiubo"
}
array(6) {
["美"]=> string(6) "Sister Feng"
["very beautiful"]=> string(12) "Sister Furong"
["verymei"]=> string(6) "Yang Mi"
[" The goddess in my heart"]=> string(6) "Hua Fei"
[100]=> string(6) "Sun Li"
[101]=> string(6) "Empress"
}

Having used the above example, we know:

Declare the associative array as key name => value

Associative arrays can have elements of index arrays

If elements without subscripts are declared after elements of index arrays in associative arrays, the maximum value +1 principle is still followed. (Observe that the values ​​​​in the above picture are the two elements of Sun Li and Empress).


Traverse associative array

To traverse and print all the values ​​in the associative array, you can use a foreach loop as follows:

Example

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


Try it »

The foreach loop will be explained in detail in the following chapters, refer to foreach in PHP for loop


Remember

The separator between array elements is a comma. When inserting an array into an array, do not write a semicolon (; ) The following picture is the wrong image display:

104.png

Multidimensional array

Multidimensional array will be introduced in detail in the PHP advanced tutorial section .

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