PHP Array

The concept of array

Array (array) is a very important concept in PHP. We can think of an array as a series of similar data collections. In fact, an array is an ordered graph.

PHP also provides more than 70 built-in functions to manipulate arrays.

Create array

Create an array using the array() language structure:

code

<?php

$arr_age1 = array(18, 20, 25);

// or:

$arr_age2 = array("wang"=>18, "li"=>20, "zhang"=>25);

// Empty array:

$arr_age3 =array();

?>

 

You can also use the array controller [] to create an array:

code

<?php

$arr_age1[] = 18;

$arr_age1[] = 20;

$arr_age1[] = 25;

// or:

$arr_age2["wang"] = 18;

$arr_age2["li"] = 20;

$arr_age2["zhang"] = 25;

?>

 

Array key and value

The array entity contains two items: key name and value.

In the following example of creating an array:

code

$arr_age1 = array(18, 20, 25);

 

We allocated 3 array elements (also called elements) to $arr_age1, the values ​​of which are 18, 20, and 25, respectively. The system will automatically assign 3 numeric serial numbers to these 3 array units, which are 0, 1, and 2. That is, the complete structure of the $arr_age1 array is:

code

Array ( [0] => 18 [1] => 20 [2] => 25 )

 

The serial number automatically assigned by the system is called the key name, and the array with the key name of the numeric ID is called the indexed array.

Of course, you can also manually specify the key name:

code

$arr_age1 = array( 0 => 18, 1 => 20, 2 => 25 );

 

prompt

The manual designation of the key name does not need to start from 0, or it does not need to specify the key name in numerical order.

When a new element is added to the array without specifying a key name, the system will automatically add 1 to the largest number key in the existing array as the key name of the new element.

When a string is used as a key name instead of a numeric index, this type of array is called an associative array:

code

$arr_age2 = array("wang"=>18, "li"=>20, "zhang"=>25);

 

But in PHP, these two types of arrays have no obvious boundary, and the two can be mixed. Note that the case of key names in associative arrays is sensitive.

Output array cell value

You can access the output array cell value as follows:

code

echo $arr_age1[0]; //Output: 18

echo $arr_age2["wang"]; //Output: 18

 

In some cases, for debugging, you may need to output the data and structure of the entire array. At this time, you need to use the print_r() or var_dump() function. For details, see " PHP Print Output Array Content and Structure print_r and var_dump Functions "

Manipulate array elements

You can manipulate array elements like ordinary variables, such as:

code

<?php

$arr_age2 = array("wang"=>18, "li"=>20, "zhang"=>25);

$arr_age2["wang"] = $arr_age2["wang"] + 10;

?>

 

Now $arr_age2 is:

code

Array ( [wang] => 28 [li] => 20 [zhang] => 25 )

 

To check if an array element is set, please use  isset()  .

Destroy array

Use the unset() function to destroy an array unit or the entire array:

code

<?php

unset($arr_age1[0]);

unset($arr_age1);

?>

 

Multidimensional Arrays

If the value in the array is also an array, we call such an array a recursive array or a multidimensional array.

example:

code

<?php

$all = array( "fruits" => array( "a"=>"orange", "b"=>"banana", "c"=>"apple"),

"ages" => array( 18, 20, 25 )

);

echo $all["fruits"]["c"];

//Output apple

echo $all["ages"][0];

//Output 18

?>

 

 

Guess you like

Origin blog.csdn.net/whm156399/article/details/108551996
Recommended