PHP Array


An array is a very important concept in PHP. We can think of an array as a collection of similar data, in fact an ordered graph.

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


Create array

Create an array using the array() language construct:

$arr_age1 = array(18, 20, 25);
// Alternatively:
$arr_age2 = array("john" => 18,"jack" => 20,"marry" => 25);
// empty array:
$arr_age3 = array();

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

$arr_age1[] = 18;
$arr_age1[] = 20;
$arr_age1[] = 25;
// Alternatively:
$arr_age2["john"] = 18;
$arr_age2["jack"] = 20;
$arr_age2["marry"] = 25;

array key name and value

array entities contain two items: key names and values.

In the following example to create an array:

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

We assign $arr_age1 three array elements(also known as elements), the value is 18,20 and 25, respectively. The system will automatically assign the three array elements of the three numbers, namely 0,1 and 2, respectively. The complete structure of the $arr_age1 array is:

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

The system automatically assigns a sequence number called a key name, and an array of keys called a numeric ID is called an indexed array.

Of course, you can manually specify the key name:

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

Tip

  • Manually specifying key names can not start with zero, or you can specify key names in numerical order.
  • When a new cell is added to an array without specifying a name, the system will automatically add 1 to the largest numeric key in the existing array as the key name for the new cell.

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

$arr_age2 = array("john" => 18,"jack" => 20,"marry" => 25);

However, in PHP, these two arrays have no obvious boundaries, the two can be mixed. Note that the associative array is case-sensitive for key names.


Output array cell values ​​

You can access the output array cell values ​​as follows:

echo $arr_age1[0]; // Output: 18
echo $arr_age2["john"]; // Output: 18

In some cases, for debugging, you may need to output the entire array of data and structure, this time need to use print_r() or var_dump() function.


Operate the array element

You can manipulate array elements as you would a normal variable, such as:

$arr_age2 = array("john" => 18,"jack" => 20,"marry" => 25);
$arr_age2["john"] = $arr_age2["john"] + 10;

Now $arr_age2 is:

array(
  [john] => 28
  [jack] => 20
  [marry] => 25
)

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


Destroy array

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

unset($arr_age1[0]);
unset($arr_age1);

Multidimensional array

If the array of values ​​is also an array, we call such an array for the recursive array or multi-dimensional array.

example:

$all = array(
  "fruits" => ("a" => "orange","b" => "banana","c" => "apple"), 
  "ages" => array(18, 20, 25)
);
echo $all["fruits"]["c"]; // Output apple
echo $all["ages"][0]; // output 18



Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook