Sixth Chapter _php Array _php Tutorial

Source: Internet
Author: User
Tags sorts
The 1.PHP supports two arrays: an indexed array (indexed array) and an associative array (associative array), which uses a number as the key, which uses a string as the key.

2. Iterating through an indexed array

2.1 For Loop statement

2.2 While Loop statement

2.3 Do...while Loop statement

2.4 foreach statement

2.5 using List ()

List () can only be used to index an array starting at 0, with the syntax in the following format:

void list (mixed $var, mixed $ ...)

< span=""> 
  
  < span="">     < span="">=< span="">(' Kimi ', 5, ' Angela '  < span="">  < span="">  < span="">  < span="">(< span="">,< span="">,< span="">)=< span=""> < span="">< span="">< span="">< span="">.' '. < span="">.' '.  ?> < span="">  < span="">  < span="">

2.6 using each () (not understood)

2.7 Mixed use list () and each () to iterate through an array (not understood)

  3. Traversing associative arrays

A foreach statement is often used to traverse

The following is an example of traversing a two-dimensional array using a foreach statement:

 
       =' Boy ' =' Name ' = ' Kimi ',' Age ' =>4,' Nickname ' = ' Kimi ',),' Girl ' =' Name ' = ' Cindy ', ' age ' =>5, ' nickname ' = ' Wind ' ),  
                       
                         (   = 
                             
                                . ' = ' + 
' ( and '    ' . ' = '. . '
'; ?

4. Operation of arrays

4.1 Checks whether the specified value exists in the array using array_search ()

 
       =(' name ' = ' Kimi ', ' age ' =>5, ' hobby ' = ' reading ', 1=>2013  = (' Kimi ', 
            
               ' 

'. Span $key Span> = (5, '

'. = (' read ', ( Span $key span>= (2013, '

'. ,

4.2 Merge one or more arrays into an array using array_merge ()

The function returns the merged array, and if the array to be merged has the same string key name, the value following the key name overrides the previous value. If the array to be merged contains the same array key name, subsequent values will not overwrite the original value, but are appended to the element.

< span=""> 
  
  < span="">     < span="">=< span="">(' name ' = ' Cindy ', ' age ' =>5  < span="">  < span="">  < span="">  < span="">  < span=""> =< span="">(' name ' = ' Kimi ', ' age ' =>4  = (0=>2000  < span="">  < span=""> < span=""> < span="">    = (0=>2001  < span="">  < span=""> < span=""> < span=""> < span="">< span="">=< span="">(< span="">,< span="">, < span="">,< span="">< span="">< span="">< span="">( < span=""> ?>  < span="">  < span="">

4.3 Splitting an array into multiple arrays using array_chunk ()

The syntax format is as follows:

Array Array_chunk (Arrray $input, int $size [, bool $preserve _keys])

Where $input is a segmented array variable, $size is the number of elements of each array divided into (the last array element can be less than $size), the optional parameter $preserve_keys defaults to False, indicating that the segmented array index will be rearranged from 0 onwards. If set to True, the split array retains the key names in the original array. One-dimensional array is split once into a two-dimensional array.

4.4 Number of occurrences of all values in the statistic array use array_count_values ()

The syntax format is as follows:

Array array_count_values (array $input)

Where $input is the array to be counted. The function returns an associative array whose key name is the value of the element in the $input array, and the key value is the number of occurrences of the value of the element in the $input array

< span=""> 
  
  < span="">     < span="">=< span="">(' Kimi ', 5, ' age ' =>5, ' name ' = ' Kimi ', 2005  < span="">  < span=""> < span="">=< span="">(< span="">< span="">< span=""> < span="">(< span="">< span="">< span="">< span="">(< < span="">   /c35>< span="">  ?>  < span=""> 

4.5 Calculate and use array_sum () for all values in the array

The syntax format is as follows:

Number Array_sum (array $array)

4.6 Delete duplicate values in array using array_unique ()

The syntax format is as follows:

Array Array_unique (array $array)

Where $array is the array to be manipulated. The function first sorts the key value as a string, retains the key name of the value that was encountered for the first time, and finally returns a new array that is not worth repeating.

4.7 counts the number of elements in an array using count ()

The syntax format is as follows:

int count (array $array [, int $mode])

Where the $array is an array to be computed, the optional parameter $mode default value is 0, which means that the number of recursive statistics elements can be set to 1 (or count_recursive), representing the number of recursive statistics elements.

< span=""> 
  
  < span="">     < span="">=  < span="">    < span="">    < span="">  ' boy '= =< span="">(' name ' = ' = ') Kimi ',  < span="">  ' nickname ' = ' K ',  < span="">  ' age ' =>5,  < span="">  ' girl ' =< span="">= (' name ' = ' Cindy '  < span="">  , ' nickname ' = ' C '  < span=""> , '  < span="">  < span="">  < span="">  age ' =>4) < span=""> ,  < span=""> '

Number of non-recursive statistical elements: '. < span="">(< span="">< span="">< span="">< span=""> '

Number of recursive statistics elements: '. < span="">(< span="">,< span="">< span="">

4.8 Array Positive sort using sort ()

The syntax format is as follows:

BOOL Sort (array & $array [, int $sort _flags])

Where the parameter $array is the array to be sorted, the optional parameter $sort_flags can be one of the following 4 settings.

· Sort_regular: Sort by normal comparison (does not change type)

· Sort_number: Comparing array elements as numbers to sort

· Sort_string: Comparing and sorting array elements as strings

· Sort_locate_string: Compares and sorts array elements as strings according to the current region (LOCATE) settings

4.9 Reverse sequence of arrays using rsort ()

    The analogy is a forward sort.

4.10 Flips the order of elements in the array using Array_reverse ()

The syntax format is as follows:

Array array_reverse (array $array [, BOOL $preserve _keys])

Where $array is an array to be flipped, the optional parameter $preserve_keys defaults to False, which means that the original key name is not preserved, and the original key name is preserved when the array element is flipped when it is set to true. (for optional parameters, there are doubts, see procedure below)

< span=""> 
  
  < span="">     < span="">=< span="">(' GDP ' = ' GDP ', ' ht ' = ' ht ', ' lq ' = ' LQ ', ' lc ' = ' LC  < span=""> ' < span="">< span="">=< span="">(< span="">,< span=""> < span="">< span="">< span=""> '

'< span="">< span="">< span="">(< span="">< span="">< /c39>< span=""> ?>

4.11 Populating an array with the given values using Array_pad ()

The syntax format is as follows:

Array Array_pad (array $input, int $pad _size, mixed $pad _value)

Where the parameter $input is the array to be filled, the parameter $pad_size is the size of the array after the fill, and the parameter $pad_value is the element to fill. If $pad_size is positive, the filled element will be on the right side of $input and vice versa.

4.12 Calculating the intersection of multiple arrays using Array_intersect ()

The syntax format is as follows:

Array Array_intersect (array $array 1 [, Array $array 2 [, array $ ...])

http://www.bkjia.com/PHPjc/626578.html www.bkjia.com true http://www.bkjia.com/PHPjc/626578.html techarticle the 1.PHP supports two arrays: an indexed array (indexed array) and an associative array (associative array), which uses a number as the key, which uses a string as the key. 2. Iterating through an array of indexes ...

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