PHP array definition and initialization learning notes

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags array arrays created data display echo etc example

Array definition and initialization

What is an array? An array is a programming structure that is a variable that holds a set of values ​​or a series of values.

For example, at the time of the census, personal identity registration, such as name, gender, nationality, birth, etc. can be used as an array.

PHP Tutorial to create an array array () structure to define, for example:

$ people = array ('name', 'sex', 'nation', 'brith');

How to display the value of each element in the array, we use the index from 0, the index number in square brackets after the variable name, such as:

<? php
$ people = array ('name', 'sex', 'nation', 'birth');
Echo $ people [2];
?>

The output of $ people [2] is displayed nation (the first index from 0 count).

In addition to supporting numeric indexed arrays, php also supports related arrays. The so-called related array, is to customize the keyword to replace the not-intuitive digital index, such as:

<? php
$ peoples = array ('xm' => 'name', 'xb' => 'sex', 'mz' => 'nation', 'cs' => 'birth');
echo $ peoples ['cs'];
?>

The use of related arrays makes the choice of output intuitive (no need to pre-calculate the index number and then output), the definition of keywords and values ​​between the use of "=>" symbol definition.

According to the two display elements of the php array element, you can also create numbers directly without variables like array () declaration and initialization. such as

$ people [0] = 'name';
$ people [1] = 'sex';
$ people [2] = 'nation';
$ people [3] = 'brith';

or

$ peoples ['xm'] = 'name';
$ peoples ['xb'] = 'sex';
$ peoples ['mz'] = 'nation';
$ peoples ['cs'] = 'birth';

The size of the array changes dynamically according to how many elements are added.

2, the display of the array element

As with $ people [2], the $ peoples ['cs'] is just an array element that outputs a known, well-defined position, and how to quickly output all or part of an array element using a looping statement Is undoubtedly the fastest way.

<? php
$ people = array ('name', 'sex', 'nation', 'birth');
for ($ i = 0; $ i <4; $ i ++)
echo "$ people [$ i]";
?>

In addition to using for loops that understand the number of loops, you can use a foreach statement that does not require a loop.

<? php
$ people = array ('name', 'sex', 'nation', 'birth');
foreach ($ people as $ xiangmu)
echo $ xiangmu;
?>

The $ xiangmu variable will save each element value in the array, followed by the display. Of course, in order to be able to distinguish between the output data can output space after the array element:

echo $ xiangmu. "";

Now look at a php filter out the duplicate data from the array

<? php
$ num = count ($ array);
if ($ num)
{
sort ($ array);
}
if ($ num! = 0)
{
$ m = $ array [0];
$ n = 0;
$ kind = 1;
echo $ array [0]. "----------";
for ($ z = 0; $ z <$ num; $ z ++)
{
if ($ m! = $ array [$ z])
{
echo $ array [$ z-1]. "";
echo $ array [$ z];
$ kind = 0;
$ m = $ array [$ z];
}
$ n ++;
$ m + +
// echo $ array [$ z]. "'/ n'";
}
if ($ kind == 1)
{
echo $ array [$ num-1]. "Duplicate data!";
}
?>


Related array functions

list ()

array_walk

Let the user-defined function can handle each element of the array.

Syntax: int array_walk (array arr, string func);

Return Value: Integer

Reference each () list ()

arsort

Sort the array values ​​from big to small.

Syntax: void arsort (array array);

Reference asort () rsort () ksort () sort ()

asort

Sort the array values ​​from small to large.

Syntax: void asort (array array);

Return Value: None

Reference arsort () rsort () ksort () sort ()

count

Count the number of elements in a variable or array.

Syntax: int count (mixed var);

Return Value: Integer

Reference end () next () prev () reset ()

each

Returns the index and value of the next element in the array.

Syntax: array each (array array);

Return Value: Array

Related Article

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.