如何使用PHP二维数组_PHP教程

php中文网
发布: 2016-07-15 13:27:21
原创
1166人浏览过

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 函数,实例二维数组数据如下:

宣小二
宣小二

宣小二:媒体发稿平台,自媒体发稿平台,短视频矩阵发布平台,基于AI驱动的企业自助式投放平台。

宣小二 21
查看详情 宣小二
<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>
登录后复制

使用如下:print_r ( array_silice_func ( $array, 5 ) )取出二维数组的5个值,结果如下:Array ( [1] => Array ( [0] => b1 [1] => i1 [2] => u1 [3] => u1 [4] => u1 ) ) 依此类推,取出所需要的数组个数。由于PHP二维数组的使用局限性,这里没有实现从二维数组某个下标开始,取出多少个数组个数的需求,不过这也是值得探讨的地方。以上就关于如何使用PHP从二维数组中取出一段指定值,希望对大家有帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446526.htmlTechArticlePHP还是比较常用的,于是我研究了一下PHP二维数组,在这里拿出来和大家分享一下,希望对大家有用使用PHP从数组中取出一段值可使用PHP内...
相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号