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 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></SPAN></LI></OL>如上二维数组,如果需要取出其中某段,需要知道取段开始与结束在数组中的位置,考虑到应用的特殊性,只从第一个数组开始,在这个二维数组中取出需要的数组个数。方法如下:
立即学习“PHP免费学习笔记(深入)”;
<OL class=dp-xml><LI class=alt><SPAN><SPAN>function array_silice_func(array $array, $limit) </SPAN></SPAN><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></SPAN><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></SPAN><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></SPAN><LI class=alt><SPAN>if ($count + $k </SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN>= $limit) </SPAN></SPAN><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></SPAN><LI class=alt><SPAN>$temp [$key] = $t; break; } </SPAN><LI class=""><SPAN>$temp [$key] = $value; $k += $count; } return $temp; } </SPAN></LI></OL>
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号