PHP study 2 Array Function

Source: Internet
Author: User

1. Differences between sort and asort in array functions: Sort changes the key name to 0, 1, and 2, no matter what your previous array is; however, during the sorting process, the asort performs sorting based on the key value, but the key name does not change and the position of the key value is moved by the followers. [Arsort is from high to low, asort is from bottom to high, ksort, krsort is sort by key name ].

 

2 count () and sizeof () are used to count the number of elements in the array.

 

3 in_array (), array_key_exists ()

 

4. quickly create an array range (), which is similar to MATLAB. php is really colorful.

 

5 array_reverse flip array_sum calculate the sum of the array array_merge merge array array_flip key name and Value

 

6 List ($ key, $ value) = each ($ bils) reset ($ ..)

 

7. Similar to the left and right functions in ASP, substr () combines the two into one.

************

String substr (string, int start, int [length])

The string parameter is the string to be operated.

The start parameter is the start position of the string to be truncated. If the start parameter is negative, it indicates that the length is truncated from the last start.

The optional parameter length is the length of the string to be truncated. If it is not specified during use, it is obtained to the end of the string by default. If the length is negative, it indicates the position of the last length from start to right.

It may be awkward to use this function at first, But if you understand the syntax of the PHP substr function, its functions are better than the left and right functions in ASP. The following is an example of his usage:

1. truncate from 4th characters to the end of the string, similar to left in ASP:

<? PHP $ STR = "www.designline.cn"; echo substr ($ STR, 4);?>

Output: designline.cn

2. the PHP substr function intercepts three characters from the right side, similar to the right in ASP:

<? PHP $ STR = "www.designline.cn"; echo substr ($ STR,-3);?>

Output:. cn

3. the PHP substr function intercepts 6 characters starting from 4th characters:

<? PHP $ STR = "www.designline.cn"; echo substr ($ STR, 4, 6);?>

Output: Design

4. Sometimes we know the start and end of a string, which is an indefinite character in the middle, in this case, we can use substr in addition to the regular expression of the PHP substr function (of course, there are n character methods to be obtained in the middle. Here is just an example of the substr application ):

<? PHP $ STR = "<|> www.designline.cn <|>"; echo substr ($ STR, 3,-3);?>

Output: www.designline.cn

************

 

Attach the used example:

 

**************************************** **************************************** **********************

 

<? PHP

$ Seasons = array ("C", "X", "Q", "D ");

Echo "Hello array/N ";

Print_r ($ seasons );

Echo "<br> ";

$ City = array (

"Hebei" => array ("Beijing A1", "Baoding A2 "),

"Henan B" => array ("B Zhengzhou 1", "B Luoyang 2 ")

);

Print_r ($ City );

 

// Index the Array

$ Index = array (

1 => "one ",

2 => "two ",

3 => "three"

);

Echo "<br> ";

Print_r ($ index );

Echo "<br> ";

Echo $ Index [1];

 

 

// Associate an array

$ Language ['qiao'] = "Qiao ";

$ Language ['zhang '] = "Zhang ";

Echo "<br> ";

Echo "Qiao is named". $ language ['qiao']. "<br> ";

Echo "Zhang is named". $ language ['zhang '];

 

// Basic Run

Foreach ($ language as $ key => $ value ){

Echo "<br> $ key -- $ value <br> ";

}

 

// Foreach twice

Foreach ($ City as $ key => $ value)

{

Foreach ($ value as $ value2)

{

Echo "<br> $ key --- $ value2 <br> ";

}

}

 

For ($ I = 1; $ I <3; $ I ++)

{

$ Value = $ Index [$ I];

Echo "<br> -- $ I -- $ value -- <br> ";

}

 

// Add an array

$ Ball = array ("football", "soccer ");

$ Ball [] = "basketball ";

Echo "<br> ";

Print_r ($ ball );

 

 

// Delete

Unset ($ ball [2]);

Function myprint ($)

{

Echo "<br> ";

Print_r ($ );

}

Myprint ($ ball );

 

// Update

$ Ball [2] = "basketba ";

Myprint ($ ball );

$ Ball [2] = "basketball ";

Myprint ($ ball );

 

// Sort

$ Color = array ("yellow", "Red", "green", "gray ");

Sort ($ color );

Myprint ($ color );

$ Number = array (4,342, 32 );

Myprint ($ number );

Sort ($ number, sort_numeric );

Myprint ($ number );

 

Foreach ($ number as $ value)

{

Echo $ value ."";

}

 

// If the key name is specified, sort may cause disorder.

$ Cl = array ("Yel" => "yellow", "re" => "red", "Ge" => "green ", "Bl" => "blue ");

$ Cl2 = $ Cl;

Sort ($ Cl );

Foreach ($ Cl as $ key => $ value)

{

Echo "<br> $ key -- $ value -- <br> ";

Echo "<br> *************** <br> ";

}

Asort ($ Cl2 );

 

Function printa ($)

{

Foreach ($ A as $ key => $ value)

{

Echo "<br> ****************** <br> ";

Echo "$ key ---> $ value <br> ";

// Echo "<br> ****************** <br> ";

}

}

Myprint ($ Cl2 );

Printa ($ Cl2 );

 

// Try arsort

Arsort ($ Cl2 );

 

Echo "<br> & arsort <br> ";

 

Printa ($ Cl2 );

 

// Ksort, krsort

$ Keys = array ("B" => "A", "a" => "B", "C" => "C ");

Ksort ($ keys );

Printa ($ keys );

Krsort ($ keys );

Printa ($ keys );

 

// Random sort

Shuffle ($ keys );

Printa ($ keys );

 

 

$ Have = array ("Qiao", "Yong ");

If (in_array ("Qiao", $ have, true ))

{

Echo "<br> is in <br> ";

}

 

$ Have ['wang'] = "meinv ";

If (array_key_exists ("Wang", $ have ))

{

Echo "<br> key is in <br> ";

}

 

 

$ Num = count ($ have );

Echo $ num;

 

$ Quick = range (, 10 );

Myprint ($ quick );

 

$ Quicka = range ('A', 'D ');

Myprint ($ quicka );

 

// Reverse

 

Myprint (array_reverse ($ quick ));

 

Echo array_sum ($ quick );

// Myprint (array_merge ($ quick, $ quicka ));

$ Sum2 = array_sum (array_merge ($ quick, $ quicka ));

Echo $ sum2;

 

Myprint (array_flip ($ quick ));

 

// Each () List () reset ()

// This is very interesting.

$ Final = array (1 => "eat", 2 => "listen", 3 => "look ");

While (List ($ key, $ value) = each ($ FINAL ))

{

Echo $ key. "...". $ value;

}

Reset ($ final );

List ($ nowkey, $ nowvalue) = each ($ final );

Echo $ nowkey. "..."... $ nowvalue;

 

?>

**************************************** ***************************

 

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.