Methods and details of array traversal in php (note)

Source: Internet
Author: User
Php array traversal method and details (note) // traverse the array

$ N = array (5, 7, 9, 1, 6, 2, 3 );

$ Sum = 0;

Echo"
Total ". count ($ n)." count
"; // Calculate the number of elements in the array

For ($ I = 0; $ I

{

Echo $ n [$ I];

$ Sum = $ sum + $ n [$ I];

}

Echo"
The total number is ". $ sum;

/*************************************** *************************/

// When an array is created, no subscript is specified for an element. php will automatically add a subscript (keyword) as the element with the largest subscript (integer)

$ Arr = array (5 => "logo", 567,580 );

Echo"
". $ Arr [5]; // The output value is the logo.

// $ Arr [5] = "yes"; replace the value of an element

Echo"
". $ Arr [6]; // The output value is 567.

Echo"
". $ Arr [7]."
"; // The output value is 580.

/*************************************** *************************/

$ Ar [true] = "hello"; // = $ ar [1]

$ Ar [false] = "word"; // = $ ar [0]

$ Ar [null] = "Hefei"; // = $ ar [""] null in php in many cases <=> ""

/*************************************** *************************/

// We can usually use print_r to display this array. var_dump provides more comprehensive information.

"
". Print_r ($ arr );

Echo"
";

"
". Var_dump ($ arr );

/*************************************** *************************/

$ Arra = array (2, 3 );

// Php arrays can be dynamically increased

$ Arra [2] = 56;

Echo"
". $ Arra [2];

/******************* Several important functions in php array ************ *********/

Echo"
The array length is ". count ($ n); // counts the number of arrays.

Echo"
";

Echo is_array ($ n); // determines whether the given variable is an array. if the returned value is 1, the others are false.

Echo"
";

Print_r ($ n); // display the array

Echo"
";

Var_dump ($ n); // display the array, including data types that are more comprehensive than print_r

Echo"
";

$ Str = "Shushan Baohe Yaohai Kaikai ";

$ S = explode ("", $ str); // used to split ("") and who ("$ str, after splitting, $ s is already an array.

Print_r ($ s );

Echo"
";

Echo is_array ($ s); // $ s is an array.

/******************* Array traversal method *************** ****************/

$ Colors = array ('red', 'blue', 'black', 'green', 'yellow ');

For ($ I = 0; $ I

{

Echo"
$ Colors [$ I] ";

}

Echo"
";

$ I = 0;

While ($ I

{

Echo"
$ Colors [$ I] ";

$ I ++;

}

Echo"
";

Foreach ($ colors as $ k => $ v)

{

Echo"
$ K = $ v ";

}

// Unset ($ colors [2]); delete array $ colors subscript 2 element subscript 2 after deletion the subscript will not move up to fill the gap

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.