Home > Backend Development > PHP Tutorial > Chapter 6_PHP Array_PHP Tutorial

Chapter 6_PHP Array_PHP Tutorial

WBOY
Release: 2016-07-13 17:17:32
Original
860 people have browsed it

1. PHP supports two types of arrays: indexed array and associative array. The former uses numbers as keys, and the latter uses strings as keys.

 2. Traverse the index array

 2.1 for loop statement

 2.2 while loop statement

2.3 do...while loop statement

 2.4 foreach statement

 2.5 Use list()

List() can only be used for index arrays whose subscripts start from 0. The syntax format is as follows:

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

<span 1</span> <?<span php
</span><span 2</span>     <span $myarray</span>=<span array</span>('Kimi',5,'Angela'<span );
</span><span 3</span>     
<span 4</span>     <span list</span>(<span $nickname</span>,<span $age</span>,<span $name</span>)=<span $myarray</span><span ;
</span><span 5</span>     <span echo</span> <span $nickname</span>.' '.<span $age</span>.' '.<span $name</span><span ;
</span><span 6</span> ?>
Copy after login

 2.6 Use each() (don’t understand)

 2.7 Mixed use of list() and each() to traverse the array (don’t understand)

 3. Traverse associative array

Often use the foreach statement to traverse

The following is an example of using the foreach statement to traverse a two-dimensional array:

<span  1</span> <?<span php
</span><span  2</span>     <span $myarray</span>=<span array</span><span (
</span><span  3</span>         'boy'=><span array</span><span (
</span><span  4</span>             'name'=>'Kimi',
<span  5</span>             'age'=>4,
<span  6</span>             'nickname'=>'kimi',
<span  7</span>         ),
<span  8</span>         'girl'=><span array</span><span (
</span><span  9</span>             'name'=>'Cindy',
<span 10</span>             'age'=>5,
<span 11</span>             'nickname'=>'wind'
<span 12</span>         ),   
<span 13</span> <span     );
</span><span 14</span>     <span foreach</span>(<span $myarray</span> <span as</span> <span $gender_key</span> => <span $gender_value</span><span ){
</span><span 15</span>         <span echo</span> <span $gender_key</span>.' => <br>'<span ;
</span><span 16</span>         
<span 17</span>         <span foreach</span>(<span $gender_value</span> <span as</span> <span $key</span> => <span $value</span><span ){
</span><span 18</span>             <span echo</span> '&nbsp&nbsp'.<span $key</span>.' => '.<span $value</span>.'<br>';    <span //</span><span &nbsp似乎是增加一个空格</span>
<span 19</span> <span         }
</span><span 20</span> <span     }
</span><span 21</span> ?>
Copy after login

 4. Array operations

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

<span  1</span> <?<span php
</span><span  2</span>     <span $myarray</span>=<span array</span>('name'=>'Kimi','age'=>5,'hobby'=>'reading',1=>2013<span );
</span><span  3</span>     
<span  4</span>     <span $key</span>=<span array_search</span>('Kimi',<span $myarray</span><span );
</span><span  5</span>     <span echo</span> '<p>'.<span $key</span><span ;
</span><span  6</span>     
<span  7</span>     <span $key</span>=<span array_search</span>(5,<span $myarray</span><span );
</span><span  8</span>     <span echo</span> '<p>'.<span $key</span><span ;
</span><span  9</span>     
<span 10</span>     <span $key</span>=<span array_search</span>('read',<span $myarray</span><span );
</span><span 11</span>     <span var_dump</span>(<span $key</span><span );
</span><span 12</span>     
<span 13</span>     <span $key</span>=<span array_search</span>(2013,<span $myarray</span><span );
</span><span 14</span>     <span echo</span> '<p>'.<span $key</span><span ;
</span><span 15</span> ?>
Copy after login

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

  The function returns the merged array. If the arrays to be merged have the same string key name, the value after the key name will overwrite the previous value. If the arrays to be merged contain the same array key name, the subsequent value will not overwrite the original value, but will be appended to the element.

<span 1</span> <?<span php
</span><span 2</span>     <span $girl</span>=<span array</span>('name'=>'Cindy','age'=>5<span );
</span><span 3</span>     <span $boy</span>=<span array</span>('name'=>'Kimi','age'=>4<span );
</span><span 4</span>     <span $girl_date</span>=<span array</span>(0=>2000<span );
</span><span 5</span>     <span $boy_date</span>=<span array</span>(0=>2001<span );
</span><span 6</span>     
<span 7</span>     <span $myarray</span>=<span array_merge</span>(<span $girl</span>,<span $boy</span>,<span $girl_date</span>,<span $boy_date</span><span );
</span><span 8</span>     <span var_dump</span>(<span $myarray</span><span );
</span><span 9</span> ?>
Copy after login

 4.3 Split an array into multiple arrays using array_chunk()

The syntax format is as follows:

 array array_chunk(arrray $input, int $size [, bool $preserve_keys])

  Among them, $input is the divided array variable, $size is the number of elements of each divided array (the elements of the last array can be less than $size), and the optional parameter $preserve_keys defaults to false, indicating that after division The array index will be rearranged starting from 0. If set to true, the split array will retain the key names in the original array. A one-dimensional array is divided into a two-dimensional array.

 4.4 To count the number of occurrences of all values ​​in an array, use array_count_values()

The syntax format is as follows:

 array array_count_values(array $input)

  Among them, $input is the array to be counted. This function returns an associative array whose key name is the value of the element in the $input array. The key value is the number of times the element's value appears in the $input array

<span 1</span> <?<span php
</span><span 2</span>     <span $boy</span>=<span array</span>('Kimi',5,'age'=>5,'name'=>'Kimi',2005<span );
</span><span 3</span>     <span $counts</span>=<span array_count_values</span>(<span $boy</span><span );
</span><span 4</span>     <span print_r</span>(<span $counts</span><span );
</span><span 5</span>     <span var_dump</span>(<span $counts</span><span );
</span><span 6</span> ?>
Copy after login

 4.5 To calculate the sum of all values ​​in an array use array_sum()

The syntax format is as follows:

 number array_sum(array $array)

 4.6 To delete duplicate values ​​in an array, use array_unique()

The syntax format is as follows:

 array array_unique(array $array)

  Among them, $array is the array to be operated on. This function first sorts the key values ​​as strings, retains the key name corresponding to the first encountered value, and finally returns a new array with no duplicate values.

 4.7 To count the number of elements in an array, use count()

The syntax format is as follows:

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

  Among them, $array is the array to be calculated. The default value of the optional parameter $mode is 0, which means that the number of elements will not be counted recursively. It can be set to 1 (or COUNT_RECURSIVE), which means the number of elements will be counted recursively.

<span  1</span> <?<span php
</span><span  2</span>     <span $myarray</span>=<span array</span><span (
</span><span  3</span>         'boy'=><span array</span>('name'=>'Kimi',
<span  4</span>                      'nickname'=>'K',
<span  5</span>                      'age'=>5),
<span  6</span>         'girl'=><span array</span>('name'=>'Cindy',
<span  7</span>                       'nickname'=>'C',
<span  8</span>                       'age'=>4),
<span  9</span> <span         );
</span><span 10</span>     
<span 11</span>     <span echo</span> '<P>不递归统计元素个数:'.<span count</span>(<span $myarray</span><span );
</span><span 12</span>     <span echo</span> '<p>递归统计元素个数:'.<span count</span>(<span $myarray</span>,<span COUNT_RECURSIVE);
</span><span 13</span> ?>        
Copy after login

 4.8 Use sort()

to forward sort the array

The syntax format is as follows:

bool sort( array &$array [, int $sort_flags])

  Among them, the parameter $array is the array to be sorted, and the optional parameter $sort_flags can be one of the following four settings.

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

 ·SORT_NUMBER: Sort by comparing array elements as numbers

 ·SORT_STRING: Compare and sort array elements as strings

 ·SORT_LOCATE_STRING: Compares array elements as strings and sorts them according to the current location (locate) setting

 4.9 Use rsort()

to reverse sort the array

  Analogous forward sorting.

 4.10 Use array_reverse()

to reverse the order of elements in an array

The syntax format is as follows:

 array array_reverse( array $array [, bool $preserve_keys])

  Among them, $array is the array to be flipped. The optional parameter $preserve_keys defaults to FALSE, which means that the original key names are not retained. When set to TRUE, it means that the original key names are retained when flipping the array elements. (There are doubts about optional parameters, see the procedure below)

<span 1</span> <?<span php      
</span><span 2</span>     <span $myarray</span>=<span array</span>('gdp'=>'GDP','ht'=>'HT','lq'=>'LQ','lc'=>'LC'<span );
</span><span 3</span>     <span $new_array</span>=<span array_reverse</span>(<span $myarray</span>,<span FALSE</span><span );//无论是FALSE还是TRUE,键名依旧未改变
</span><span 4</span>     <span echo</span> '<p>'<span ;
</span><span 5</span>     <span print_r</span>(<span $new_array</span><span );
</span><span 6</span> ?>
Copy after login

 4.11 Fill an array with a given value using array_pad()

The syntax format is as follows:

 array array_pad(array $input, int $pad_size, mixed $pad_value)

  Among them, the parameter $input is the array to be filled, the parameter $pad_size is the size of the array after filling, and the parameter $pad_value is the element used to fill. If $pad_size is positive, padded elements will be to the right of $input and vice versa.

 4.12 Calculate the intersection of multiple arrays using array_intersect()

The syntax format is as follows:

 array array_intersect( array $array1 [, array $array2 [, array $...] ] )

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/626578.htmlTechArticle1.PHP supports two types of arrays: indexed array and associative array. The former uses Numbers as keys, the latter uses strings as keys. 2. Traverse the index array...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template