Summary of PHP array usage

Source: Internet
Author: User
Tags foreach array array sort arrays count join key mysql
In PHP, arrays are grouped into two categories: an indexed array and an associative array. They can be used separately or in combination.

1, one-dimensional array

The definition of one-dimensional arrays is also very simple, commonly used in the following two ways:

1.1 Direct Assignment

1:
 -->
   2: $dwqs [0] = "1";
   3: $DWQS [1] = "my blog:";
   4: $DWQS [2] = "www.ido321.com";
   5: $DWQS [3] = "program enthusiast:";
   6: $dwqs [4] = "QQ Group: 259280570";
   7: $dwqs [5] = "Welcome to join";
   8:?>

1.2 Array () to build arrays

1:
  -->
   2: $dwqs = Array (1, "My Blog", "Www.ido321.com", "Program Enthusiasts:", "QQ Group: 259280570", "welcome you to join");
   3:?>

2, multidimensional array

Take an associative array as an example

1:
   -->
   2: $dwqs 1= Array (
   3:     "numbered" => Array (1,2,3),
   4:     "blog" => Array ("Independent blog", "CSDN", "blog Park"),
   5:     "Address" => Array ("www.ido321.com", "blog.csdn.net/u011043843", "www.cnblogs.com/ido321")  
   6:?>
Second, the array traversal in PHP, the commonly used array traversal method has three kinds: 1, for loop
1:
    -->
   2:for ($i = 0; $i < count ($DWQS); $i + +) {
   3:     echo "$dwqs [i]
";
   4:?>

2, the foreach statement

   1://The first way
2:
     -->
   3:foreach ($dwqs as $value) {
   4:     echo "$value
";
   5:?>
   6:  
   7://Second Way
8:
      -->
   9:foreach ($dwqs 1 as $key => $value) {
       echo $key. " => ". $value;
  One:?>

3, while loop

1:
       -->
   2:while (List ($key, $value) = each ($dwqs 1)) {
   3:     echo $key. ":" $value;
   4:?>

Partial array-related functions (using the Print_r () function to output the array contents)

The PHP array is very powerful and is one of the most commonly used data types. Its processing function also has the powerful, the high efficiency characteristic.

1, the array of key/value operation function

1.1 Function Array_values (): Returns the value of all elements in an array. The array name is passed in, the key name is not preserved, and the returned array is indexed from 0.

1:
        -->
   2: $dwqs 2 = Array ("ID" => 1, "blog" => "www.ido321.com", "Program Enthusiasts" => "QQ Group: 259280570");
   3://output: Array ([0]=>1,[1]=>www.ido321.com,[2]=>qq Group: 259280570)
   4:print_r (Array_values ($dwqs 2));
   5://output: Array ("ID" => 1, "blog" => "www.ido321.com", "Program Enthusiasts" => "QQ Group: 259280570");
   6:print_r ($dwqs 2);
   7:  
   8:?>

1.2 Function Array_keys (): Returns the key name in the array.

1:
         -->
   2://Output all key names: Array ([0]=>id,[1]=> blog,[2]=> program enthusiasts);
   
   4://Output specified key name: Array ([0]=>id)
   5:print_r (Array_kays ($dqws, "ID"));
   6:?>

1.3 function In_array (): Detecting whether a value exists in the array

1:
          -->
   2: $address = "www.ido321.com";
   3://output: Exist
   4:if (In_array ($address, $dwqs 2)) {
   5:     echo "existence";
   6:}
   7:else{
   8:     echo "does not exist";
   9:}
  Ten:?>
2, array number and Uniqueness 2.1 function count (): Count the number of elements in an array or the number of properties in an object
1:
           -->
   2:     echo Count ($dwqs 2);
   3:?>

2.2 Function Array_unique (): Deletes the duplicate value in the array, the returned array key name remains unchanged

 1: "!--? php 
 2: $a = Array ("A" => "PHP", "B" => "MySQL", "C" => "Linux", "D" => "PHP"); 
 3://Output: Array ("a" => "PHP", "B" => "MySQL", "C" => "Linux"); 
 4:print_r (Array_unique ($a)); 
 5: 

2.3 Function Array_count_values (): Counts the number of occurrences of all values in the array, returns the array with the value in the original array as the key, and the key value is the number of times the element appears in the original array

 1: "!--? php 
 2://output: Array (php => 2,mysql => 1,linux => 1) 
 3:print_r (array_count_values ($a)); 
 4: 
3, array sort
HTML


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.