Rumah > Artikel > pembangunan bahagian belakang > 如何使用PHP二维数组_PHP教程
PHP还是比较常用的,于是我研究了一下PHP二维数组,在这里拿出来和大家分享一下,希望对大家有用使用PHP从数组中取出一段值可使用PHP内置函数array_slice(),但这个函数只支持一维数组,具体使用可查看PHP手册,array_slice函数并不支持二维数组,。首先了解一下array_slice函数:
<ol class="dp-xml"><li class="alt"><span><span>array array_slice ( array array, int offset [, int length [, bool preserve_keys]] ) array_slice() </span></span></li></ol>
返回根据 offset 和 length 参数所指定的 array 数组中的一段序列。
实例:从一维数组中取出一段值
<ol class="dp-xml"><li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> $</span><span class="attribute"><font color="#ff0000">array</font></span><span class="attribute-value"><font color="#0000ff">array</font></span><span> = array('b','i','u','u','u'); $</span><span class="attribute"><font color="#ff0000">result</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array_slice</font></span><span>($array,0,4); print_r($result); </span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span></span></li></ol>
实例从数组$array中取出四个值,起点数组下标0开始,结果如下:从一维数组取出一段值非常简单,直接使有内置函数array_slice 。PHP二维数组的取值同样需要使用array_slice 函数,实例二维数组数据如下:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">array</font></span><span class="attribute-value"><font color="#0000ff">array</font></span><span> = array (); </span></span></li> <li class=""> <span>$array [1] = array ('1' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'b1', '2' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'i1', '3' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u1', '4' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u1', '5' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u1' );<br>$array [2] = array ('1' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'b2', '2' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'i2', '3' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u2', '4' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u2', '5' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u2' ); <br>$array [3] = array ('1' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'b3', '2' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'i3', '3' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u3', '4' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u3', '5' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u3' );<br>$array [4] = array ('1' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'b4', '2' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'i4', '3' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u4', '4' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u4', '5' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u4' ); <br>$array [5] = array ('1' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'b5', '2' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'i5', '3' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u5', '4' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u5', '5' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u5' ); <br>$array [6] = array ('1' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'b6', '2' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'i6', '3' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u6', '4' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u6', '5' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u6' ); <br>$array [7] = array ('1' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'b7', '2' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'i7', '3' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u7', '4' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u7', '5' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'u7' ); </span> </li> </ol>
如上二维数组,如果需要取出其中某段,需要知道取段开始与结束在数组中的位置,考虑到应用的特殊性,只从第一个数组开始,在这个二维数组中取出需要的数组个数。方法如下:
<ol class="dp-xml"> <li class="alt"><span><span>function array_silice_func(array $array, $limit) </span></span></li> <li class=""> <span>{ $</span><span class="attribute"><font color="#ff0000">k</font></span><span> = $</span><span class="attribute"><font color="#ff0000">count</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">0</font></span><span>; $</span><span class="attribute"><font color="#ff0000">temp</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span> (); </span> </li> <li class="alt"> <span> foreach ( $array as $</span><span class="attribute"><font color="#ff0000">key</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $value ) </span> </li> <li class=""> <span>{ $</span><span class="attribute"><font color="#ff0000">count</font></span><span class="attribute-value"><font color="#0000ff">count</font></span><span> = count ( $value ); </span> </li> <li class="alt"> <span>if ($count + $k </span><span class="tag"><strong><font color="#006699">></font></strong></span><span>= $limit) </span> </li> <li class=""> <span>{ $</span><span class="attribute"><font color="#ff0000">t</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array_slice</font></span><span> ( $value, 0, $limit - $k ); </span> </li> <li class="alt"><span>$temp [$key] = $t; break; } </span></li> <li class=""><span>$temp [$key] = $value; $k += $count; } return $temp; } </span></li> </ol>