Php array use summary_php tutorial

Source: Internet
Author: User
Php array usage summary. Php array summary in PHP, arrays are divided into two types: index array and associated array. The two can be used separately or in combination. 1. the definition of one-dimensional array is also very simple. Summary of using php arrays
In PHP, arrays are divided into two types: index arrays and associated arrays. The two can be used separately or in combination.

1. one-dimensional array

The definition of one-dimensional arrays is also very simple. There are two common methods:

1.1 Direct assignment

1:
 
   2: $dwqs[0] = "1“;
3: $ dwqs [1] = "my blog :";
   4: $dwqs[2] = "www.ido321.com";
5: $ dwqs [3] = "programmers :";
6: $ dwqs [4] = "QQ group: 259280570 ";
7: $ dwqs [5] = "welcome to join ";
   8: ?>

1.2 Array () build an Array

1:
  
2: $ dwqs = array (1, "my blog", "www.ido321.com", "program lovers:", "QQ group: 259280570", "Welcome to join ");
   3: ?>

2. multi-dimensional array

Take the joined array as an example

1:
   
   2: $dwqs1= array(
3: "number" => array (1, 2, 3 ),
4: "blog" => array ("Independent blog", "CSDN", "blog Garden "),
5: "address" => array ("www.ido321.com", "blog.csdn.net/u011043843", "www.cnblogs.com/ido321 ")
   6: ?>
2. array traversal in PHP, there are three common array traversal methods: 1. for loop
1:
    
   2: for($i = 0; $i < count($dwqs); $i++){
   3:     echo "$dwqs[i]
";
   4: ?>

2. foreach statement

1: // method 1
2:
     
   3: foreach($dwqs as $value){
   4:     echo "$value
";
   5: ?>
   6:  
7: // method 2
8:
      
   9: foreach($dwqs1 as $key=>$value){
  10:     echo $key."=>".$value;
  11: ?>

3. while loop

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

3. some array-related functions (use the print_r () function to output the array content)

PHP arrays are very powerful and are one of the most commonly used data types. Its processing functions are also powerful and efficient.

1. array key/value operation functions

1.1 function array_values (): returns the values of all elements in the array. You can pass in the array name without retaining the key name. The Returned array starts from 0 and then re-creates the index.

1:
        
2: $ dwqs2 = array ("ID" => 1, "blog" => "www.ido321.com", "programmers" => "QQ group: 259280570 ");
3: // output: Array ([0] => 1, [1] => www.ido321.com, [2] => QQ group: 259280570)
   4: print_r(array_values($dwqs2));
5: // output: array ("ID" => 1, "blog" => "www.ido321.com", "programmers" => "QQ group: 259280570 ");
   6: print_r($dwqs2);
   7:  
   8: ?>

1.2 function array_keys (): return the key name in the array.

1:
         
2: // output all key names: Array ([0] => ID, [1] => blog, [2] => programmers );
   3: print_r(array_keys($dwqs2)) 
4: // output the specified key name: Array ([0] => ID)
   5: print_r(array_kays($dqws,'ID'));
   6: ?>

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

1:
          
   2: $address = "www.ido321.com";
3: // output: Exists
   4: if(in_array($address,$dwqs2)){
5: echo "exists ";
   6: }
   7: else{
8: echo "does not exist ";
   9: }
  10: ?>
2. number of arrays and uniqueness 2.1 function count (): counts the number of elements in an array or the number of attributes in an object.
1:
           
   2:     echo count($dwqs2);
   3: ?>

2.2 function array_unique (): deletes repeated values in the array, and the returned array key name remains unchanged.

1:
            
   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. the returned array uses the value in the original array as the key name, and the key value is the number of occurrences of the element in the original array.

1:
             
2: // output: Array (php => 2, mysql => 1, linux => 1)
   3: print_r(array_count_values($a));
   4: ?>
3. array sorting
Html

In PHP, arrays are divided into two types: index arrays and associated arrays. The two can be used separately or in combination. 1. the definition of one-dimensional array is also very simple...

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.