PHP Array Basic Operation method detailed _php Example

Source: Internet
Author: User

The concept of an array

Array is a very important concept in PHP, we can think of arrays as a set of similar data, in fact, the array is an ordered graph.

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


Create an array

To create an array using the array () language structure:

<?php
$arr _age1 = Array (a);
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:

<?php
$arr _age1[] =;
$arr _age1[] =;
$arr _age1[] =;
Or:
$arr _age2["Wang"] =;
$arr _age2["li"] =;
$arr "Zhang"] = _age2[;
? >

Array key names and values

Array entities contain two items: The key name and the value.

In the following example of creating an array:

$arr _age1 = Array (18, 20, 25) We assign 3 array units (also called elements) to the $arr _age1, which are 18, 20, and 25 respectively. The system automatically assigns the 3 array cells to 3 numeric ordinal numbers, 0, 1, and 2, respectively. That is $arr the complete structure of the _age1 array is:

Array ([0] => [1] => [2] => 25) This system automatically assigns an ordinal number called a key name, an array of numeric IDs called the index Array (indexed array).

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

$arr _age1 = Array (0 =>, 1 =>, 2 => 25);

Note: Manually specifying key names may not start at 0, or you can specify key names without a numeric order. When a new cell joins an array without specifying a health name, the system automatically adds 1 to the largest number key in the existing array as the key for the new cell.

This array is called an associative array (associative array) when a string is used instead of a numeric index as the key name:

$arr _age2 = Array ("Wang" =>18, "Li" =>20, "Zhang" =>25), but in PHP, there is no obvious boundary between the two arrays, which can be mixed. Note the associative array is sensitive to the case of the key name.

Output array Cell value

You can access the output array cell values in the following ways:

echo $arr _age1[0]; Output: 18
echo $arr _age2["Wang"]; Output: 18 In some cases, in order to debug, you may need to output the entire array of data and structure, this time need to use the Print_r () or var_dump () function, specifically see the PHP print_r and var_dump output array

Manipulating array cells

You can manipulate an array unit like a normal variable, such as:

<?php
$arr _age2 = Array ("Wang" =>18, "Li" =>20, "Zhang" =>25);
$arr _age2["Wang"] = $arr _age2["Wang"] +;
? >

Now $arr _age2 is:

Array ([Wang] => [Li] => [Zhang] => 25) Check that an array unit is set, please use Isset ().

Destroying an array

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

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

<?php
$all = Array ("Fruits" => Array ("A" => "Orange", "B" => "banana", "C" => "Apple"),
	"ages" => Array (m,)
	);
echo $all ["Fruits"] ["C"];	Output Apple
echo $all ["Ages"][0];		Output
?>

The above is a small series for everyone to bring the basic operation of the PHP array to explain all the content, I hope that many support cloud Habitat Community ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.