PHP Array in detail, an array of detailed _php tutorials

Source: Internet
Author: User

PHP array, detailed array


As a C + + programmer, in the process of PHP development, the PHP array generated some confusion, and C + + arrays have similar places, there are some differences, the following comprehensive analysis of the PHP array and its corresponding data types in C + + and the relationship between.

Classification of arrays:

1, array of values: Also known as the index array, that is, the number (starting from 0) as an array subscript. Equivalent to a vector in C + +.

2. Associative array: Subscript the array as a string. Corresponds to a map in C + +.

3. Multidimensional arrays: Each element in an array is also an array. Each element in its sub-array can also be an array.

Declaration of the array:

1. Array of values

A, in the following example, the numeric ID key is assigned automatically.   $names Array ("Peter", "Joe", "Lily");  b, in the example below, we manually assign the numeric ID key.   $names [0] = "Peter";   $names [1] = "Joe";   $names [2] = "Lily";  These ID keys can be used in scripts:  
 
   php      $names[0] = "Peter";       $names [1] = "Joe";       $names [2] = "Lily";       Echo $names [0]. " and ". $names [1]. " Is ". $names [2]. "' S neighbors ";   /*  */?>  

2. Associative arrays:

Example 1   $ages Array ("Peter" =>32, "Joe" =>30, "Lily" =>28);  Example 2 This example is the  same as Example 1, just another way to create an array.   $ages ["Peter"] = "+";   $ages ["Joe"] = "a";   $ages ["Lily"] = "";  Use associative arrays in scripts:  
 
   php      $ages["Peter"] = "       +"; $ages ["Joe"] = "a";       $ages ["Lily"] = "";       Echo "Peter is". $ages ["Peter"]. " Years old. " ;   /*  *   /?> above script output  :

3. Multidimensional Arrays:

In this example, we created a multidimensional array with an auto-assigned numeric ID key:   $families Array   {      "Griffin" = =array          {"Peter",          "Lois",          "Megan"       },      "quagmire" = =array          {"Glenn"      },      " Brown "= =Array      {          " Cleveland ",          " Loretta ",          " Junior "      }  };   Echo $families [' Griffin '] [2]. "A part of the Griffin family?" ;  The above code output: is  Megan apart of the Griffin family?  

Array traversal

1, the For loop traversal for loop can only traverse an indexed array. 
 Php$names=Array("Peter", "Joe", "Lily");  for($id= 0;$id<Count($names);++$id)      {          Echo $names[$id]; }  ?> 2, a foreach traversal can traverse an indexed array, or it can traverse an array of indexesforeach(array_expression as $value) {loop body; } Traversal associative arrayforeach(array_expression as $key=$value) {loop body; } A, one-dimensional array traversal index array
 Php$contact=Array("Li MoU", "xx Company", "abc@xx.com"); foreach($contact  as $value)      {          Echo $value; }  ?>
 Php$contact=Array("Name" = "Li MoU", "Company" = "xx Company", "Mailbox" = "abc@xx.com"); foreach($contact  as $key=$value)      {          Echo $key.":".$value; }  ?>B, multidimensional array traversal
 Php$wage=Array(          "Marketing Department" =Array(              Array(1, "Li MoU", "Marketing Manager", 8000),Array(2, "Wang MoU", "Market commissioner", 5000),Array(3, "Liu MoU", "Market commissioner", 7000)          ), "product Department" =Array(              Array(1, "Lee", "Product Manager", 9000),Array(2, "Wang MoU", "Product Specialist", 6000),Array(3, "Liu MoU", "Product Specialist", 5000)          ), "accounts Department" =Array(              Array(1, "Lee", "Account Manager", 7000),Array(2, "Wang MoU", "Account Officer", 6000),Array(3, "Liu MoU", "Account Officer", 5000)          )      ); foreach($wage  as $section=$table)      {          Echo $section." Department personnel as follows "; foreach($table  as $row)          {              foreach($row  as $value)              {                  Echo $value; }          }      }  /*how to ask Hovertree.com*/?>

Recommendation: http://www.cnblogs.com/roucheng/p/phpdongtai.html

http://www.bkjia.com/PHPjc/1137446.html www.bkjia.com true http://www.bkjia.com/PHPjc/1137446.html techarticle PHP Array, detailed array as a C + + programmer, in the process of PHP development, the PHP array generated some confusion, and C + + array has a similar place, there are some differences, the next ...

  • 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.