PHP Language--arrays

Source: Internet
Author: User

1. Array definitions

defined in different ways

You can put any type of data inside the array.

1). Indexed array

$attr Array ("AA");
Print_r ($ATTR);

2). Associative arrays

$attr Array ("One" =>10, "one" and "a" = "AA", "three" =>20); // Key=>value Print_r ($attr); Echo $attr // associative arrays can only be taken with key

2. Iterating through an array

For loop traversal, can only traverse indexed array, cannot traverse associative array

$attr Array ("AA");  for ($i$i<count($attr); $i+ +)  {echo$attr[$i];}

foreach traversal, which can traverse associative arrays and indexed arrays

// traversing values only foreach ($attr as$value) {echo$value;} // traversing keys and values  for ($attras$key= +$value) {echo$key ." = ". $value ;}

each () function list () function

//each () function, go to an element inside the array, take the value down, and return the element as an array$attr=Array("One" =>10, "2" = "AA", "three" =>20);Print_r( each($attr));Print_r( each($attr));Print_r( each($attr));Echo"------------------------"Reset($attr);//pointer Reset, pointing to the first element

 // list () function   $attr  = array  (1,2,3,4, "AA" ); list  ( $a ,  $b ,  $c ) =  $attr   echo   $a   echo   $b   echo   $c   list  (,,,,  $f ) = $attr  
// each () and  list () iterates through an array, indexed arrays and associative arrays can be $attrArray ("One" =>10, "2" = "AA", "three" = >20);  while (list($key,$value) =each(attr)) {  echo$key ." = ". $value. " <br> ";}

3. Pointers

 Current($attr);//takes the current pointer to the contents of the valueKey($ATTT);//index key to which the current pointer is pointingNext($attr);//move the pointer downprev($attr);//move the pointer upEnd($attr);//point pointer to last elementReset($attr); Reset the pointer//Traverse Do{Echo  Key($attr);Echo  Current($attr);Echo"<br>";} while(Next($attr))

4. Pre-defined arrays

$_server   Server and execution Environment information $_get$_post$_request$_files$_cookie $_session

PHP Language--arrays

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.