Home >php教程 >php手册 >第六章_PHP数组

第六章_PHP数组

WBOY
WBOYOriginal
2016-06-13 10:17:58861browse

  1.PHP支持两种数组:索引数组(indexed array)和关联数组(associative array),前者使用数字作为键,后者使用字符串作为键。

  2.遍历索引数组

    2.1 for循环语句

    2.2 while循环语句

    2.3 do...while循环语句

    2.4 foreach语句

    2.5使用list()

      list()只能用于下标从0开始的索引数组,语法格式如下:

        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> ?>

    2.6 使用each()(不明白)

    2.7 混合使用list()和each()遍历数组(不明白)

  3.遍历关联数组

    常常使用foreach语句来遍历

    下面是一个使用foreach语句遍历二维数组的例子:

<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> ?>

  4.数组的操作

    4.1 检查数组中是否存在指定的值使用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> ?>

    4.2 把一个或多个数组合并为一个数组使用array_merge()

       函数返回合并后的数组,如果待合并的数组中有相同的字符串键名,则该键名后面的值将覆盖前一个值。如果待合并的数组中包含相同的数组键名,后面的值将不会覆盖原来的值,而是附加到该元素后面。

<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> ?>

    4.3 把一个数组分割为多个数组使用array_chunk()

    语法格式如下:

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

    其中,$input是分割的数组变量,$size是分割成的每个数组的元素个数(最后一个数组的元素可以小于$size),可选参数$preserve_keys默认为false,表示分割后的数组索引将从0开始重新编排。若设置为true,则分割后的数组将保留原数组中的键名。一维数组分割一次变成二维数组。

    4.4 统计数组中所有值出现的次数使用array_count_values()

    语法格式如下:

    array array_count_values( array $input)

    其中,$input是待统计的数组。该函数返回一个关联数组,其键名为$input数组中的元素的值,键值为该元素的值在$input数组中出现的次数

<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> ?>

    4.5 计算数组中所有值的和使用array_sum()

    语法格式如下:

    number array_sum(array $array)

    4.6 删除数组中重复的值使用array_unique()

    语法格式如下:

    array array_unique(array $array)

    其中,$array是待操作的数组。该函数首先将键值作为字符串进行排序,保留第一次遇到的值所对应的键名,最后返回一个没有重复值得新数组。

    4.7 计算数组中的元素数目使用count()

    语法格式如下:

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

    其中,$array是待计算的数组,可选参数$mode默认值为0,表示不进行递归统计元素个数,可以设置为1(或COUNT_RECURSIVE),表示递归统计元素的数目。

<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> ?>        

    4.8 对数组正向排序使用sort()

    语法格式如下:

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

    其中,参数$array是待排序的数组,可选参数$sort_flags可以为下面4种设置之一。

    ·SORT_REGULAR:通过正常比较进行排序(不改变类型)

    ·SORT_NUMBER:将数组元素作为数字来比较进行排序

    ·SORT_STRING:将数组元素作为字符串来比较并进行排序

    ·SORT_LOCATE_STRING:根据当前的区域(locate)设置把数组元素作为字符串比较并进行排序

    4.9 对数组逆向排序使用rsort()

    类比正向排序。

    4.10 将数组中的元素顺序翻转使用array_reverse()

    语法格式如下:

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

    其中,$array是待翻转的数组,可选参数$preserve_keys默认为FALSE,表示不保留原有的键名,设置为TRUE后,表示翻转数组元素时保留原有的键名。(关于可选参数,存在疑惑,见下面程序)

<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> ?>

    4.11 用给定的值填充数组使用array_pad()

    语法格式如下:

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

    其中,参数$input是待填充的数组,参数$pad_size是填充后数组的大小,参数$pad_value是用来填充的元素。如果$pad_size为正,填充的元素将位于$input右侧,反之亦然。

    4.12 计算多个数组的交集使用array_intersect()

    语法格式如下:

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

Statement:
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