设为首页 收藏本站
查看: 707|回复: 0

[经验分享] php:获取数组第一个值

[复制链接]

尚未签到

发表于 2017-4-3 14:16:27 | 显示全部楼层 |阅读模式
php 获取数组 第一个值 <wbr><br><br><br>reset (PHP 3, PHP 4, PHP 5)<br>reset -- 将数组的内部指针指向第一个单元<br>说明<br>mixed reset ( array &amp;array )<br>reset() 将 array 的内部指针倒回到第一个单元并返回第一个数组单元的值,如果数组为空则返回 FALSE。<br><br>例 1. reset() 例子<br><div style="font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(240, 240, 240); font-family: Consolas, 'Lucida Console', 'Courier new'; color: rgb(32, 32, 32);"><span style="color: rgb(160, 176, 192); font-style: italic;">01<wbr></wbr></span><span style="color: rgb(16, 96, 160);">&lt;?php</span><br><span style="color: rgb(160, 176, 192); font-style: italic;">02<wbr></wbr></span><span style="color: rgb(91, 54, 116); font-style: italic;">$array</span><span><wbr></wbr></span><span style="color: rgb(32, 32, 32);">=</span><span><wbr></wbr></span><span style="color: rgb(0, 112, 32); font-weight: bold;">array</span>(<span style="color: rgb(64, 112, 160);">'stepone'</span><span style="color: rgb(32, 32, 32);">,</span><span><wbr></wbr></span><span style="color: rgb(64, 112, 160);">'step two'</span><span style="color: rgb(32, 32, 32);">,</span><span><wbr></wbr></span><span style="color: rgb(64, 112, 160);">'step three'</span><span style="color: rgb(32, 32, 32);">,</span><span><wbr></wbr></span><span style="color: rgb(64, 112, 160);">'step four'</span>);<br><span style="color: rgb(160, 176, 192); font-style: italic;">03<wbr></wbr></span><span style="color: rgb(160, 176, 192); font-style: italic;">//by default, the pointer is on the first element</span><br><span style="color: rgb(160, 176, 192); font-style: italic;">04<wbr></wbr></span><span style="color: rgb(0, 112, 32); font-weight: bold;">echo</span><span><wbr></wbr></span><span style="color: rgb(76, 143, 47); font-weight: bold;">current</span>(<span style="color: rgb(91, 54, 116); font-style: italic;">$array</span>)<span><wbr></wbr></span><span style="color: rgb(32, 32, 32);">.</span><span><wbr></wbr></span><span style="color: rgb(64, 112, 160);">"&lt;br/&gt;</span><span style="color: rgb(64, 112, 160);">\n</span><span style="color: rgb(64, 112, 160);">"</span>;<span><wbr></wbr></span><span style="color: rgb(160, 176, 192); font-style: italic;">// "stepone"</span><br><span style="color: rgb(248, 16, 176);">05<wbr></wbr></span><span style="color: rgb(160, 176, 192); font-style: italic;">// skip twosteps</span><br><span style="color: rgb(160, 176, 192); font-style: italic;">06<wbr></wbr></span><span style="color: rgb(76, 143, 47); font-weight: bold;">next</span>(<span style="color: rgb(91, 54, 116); font-style: italic;">$array</span>);<br><span style="color: rgb(160, 176, 192); font-style: italic;">07<wbr></wbr></span><span style="color: rgb(76, 143, 47); font-weight: bold;">next</span>(<span style="color: rgb(91, 54, 116); font-style: italic;">$array</span>);<br><span style="color: rgb(160, 176, 192); font-style: italic;">08<wbr></wbr></span><span style="color: rgb(0, 112, 32); font-weight: bold;">echo</span><span><wbr></wbr></span><span style="color: rgb(76, 143, 47); font-weight: bold;">current</span>(<span style="color: rgb(91, 54, 116); font-style: italic;">$array</span>)<span><wbr></wbr></span><span style="color: rgb(32, 32, 32);">.</span><span><wbr></wbr></span><span style="color: rgb(64, 112, 160);">"&lt;br/&gt;</span><span style="color: rgb(64, 112, 160);">\n</span><span style="color: rgb(64, 112, 160);">"</span>;<span><wbr></wbr></span><span style="color: rgb(160, 176, 192); font-style: italic;">// "stepthree"</span><br><span style="color: rgb(160, 176, 192); font-style: italic;">09<wbr></wbr></span><span style="color: rgb(160, 176, 192); font-style: italic;">//reset pointer, start again on step one</span><br><span style="color: rgb(248, 16, 176);">10<wbr></wbr></span><span style="color: rgb(76, 143, 47); font-weight: bold;">reset</span>(<span style="color: rgb(91, 54, 116); font-style: italic;">$array</span>);<br><span style="color: rgb(160, 176, 192); font-style: italic;">11<wbr></wbr></span><span style="color: rgb(0, 112, 32); font-weight: bold;">echo</span><span><wbr></wbr></span><span style="color: rgb(76, 143, 47); font-weight: bold;">current</span>(<span style="color: rgb(91, 54, 116); font-style: italic;">$array</span>)<span><wbr></wbr></span><span style="color: rgb(32, 32, 32);">.</span><span><wbr></wbr></span><span style="color: rgb(64, 112, 160);">"&lt;br/&gt;</span><span style="color: rgb(64, 112, 160);">\n</span><span style="color: rgb(64, 112, 160);">"</span>;<span><wbr></wbr></span><span style="color: rgb(160, 176, 192); font-style: italic;">// "stepone"</span><br><span style="color: rgb(160, 176, 192); font-style: italic;">12<wbr></wbr></span><span style="color: rgb(16, 96, 160);">?&gt;</span></div><br><br><br>next(PHP 3, PHP 4, PHP 5)<br>next --<wbr> 将数组中的内部指针向前移动一位<br>说明<br>mixed next ( array &amp;array )<br>返回数组内部指针指向的下一个单元的值,或当没有更多单元时返回 FALSE。<br><br>next() 和 current()的行为类似,只有一点区别,在返回值之前将内部指针向前移动一位。这意味着它返回的是下一个数组单元的值并将数组指针向前移动了一位。如果移动指针的结果是超出了数组单元的末端,则next() 返回 FALSE。<br><br>警告<br>如果数组包含空的单元,或者单元的值是 0 则本函数碰到这些单元也返回 FALSE。要正确遍历可能含有空单元或者单元值为 0的数组,参见 each() 函数。<br><br>例 1. next() 及相关函数的用法示例<br><div style="font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(240, 240, 240); font-family: Consolas, 'Lucida Console', 'Courier new'; color: rgb(32, 32, 32);"><span style="color: rgb(160, 176, 192); font-style: italic;">1<wbr></wbr></span><span style="color: rgb(16, 96, 160);">&lt;?php</span><br><span style="color: rgb(160, 176, 192); font-style: italic;">2<wbr></wbr></span><span style="color: rgb(91, 54, 116); font-style: italic;">$transport</span><span><wbr></wbr></span><span style="color: rgb(32, 32, 32);">=</span><span><wbr></wbr></span><span style="color: rgb(0, 112, 32); font-weight: bold;">array</span>(<span style="color: rgb(64, 112, 160);">'foot'</span><span style="color: rgb(32, 32, 32);">,</span><span><wbr></wbr></span><span style="color: rgb(64, 112, 160);">'bike'</span><span style="color: rgb(32, 32, 32);">,</span><span><wbr></wbr></span><span style="color: rgb(64, 112, 160);">'car'</span><span style="color: rgb(32, 32, 32);">,</span><span><wbr></wbr></span><span style="color: rgb(64, 112, 160);">'plane'</span>);<br><span style="color: rgb(160, 176, 192); font-style: italic;">3<wbr></wbr></span><span style="color: rgb(91, 54, 116); font-style: italic;">$mode</span><span><wbr></wbr></span><span style="color: rgb(32, 32, 32);">=</span><span><wbr></wbr></span><span style="color: rgb(76, 143, 47); font-weight: bold;">current</span>(<span style="color: rgb(91, 54, 116); font-style: italic;">$transport</span>);<span><wbr></wbr></span><span style="color: rgb(160, 176, 192); font-style: italic;">//$mode = 'foot';</span><br><span style="color: rgb(160, 176, 192); font-style: italic;">4<wbr></wbr></span><span style="color: rgb(91, 54, 116); font-style: italic;">$mode</span><span><wbr></wbr></span><span style="color: rgb(32, 32, 32);">=</span><span><wbr></wbr></span><span style="color: rgb(76, 143, 47); font-weight: bold;">next</span>(<span style="color: rgb(91, 54, 116); font-style: italic;">$transport</span>);<wbr><wbr><span style="color: rgb(160, 176, 192); font-style: italic;">// $mode ='bike';</span><br><span style="color: rgb(248, 16, 176);">5<wbr></wbr></span><span style="color: rgb(91, 54, 116); font-style: italic;">$mode</span><span><wbr></wbr></span><span style="color: rgb(32, 32, 32);">=</span><span><wbr></wbr></span><span style="color: rgb(76, 143, 47); font-weight: bold;">next</span>(<span style="color: rgb(91, 54, 116); font-style: italic;">$transport</span>);<wbr><wbr><span style="color: rgb(160, 176, 192); font-style: italic;">// $mode ='car';</span><br><span style="color: rgb(160, 176, 192); font-style: italic;">6<wbr></wbr></span><span style="color: rgb(91, 54, 116); font-style: italic;">$mode</span><span><wbr></wbr></span><span style="color: rgb(32, 32, 32);">=</span><span><wbr></wbr></span><span style="color: rgb(76, 143, 47); font-weight: bold;">prev</span>(<span style="color: rgb(91, 54, 116); font-style: italic;">$transport</span>);<wbr><wbr><span style="color: rgb(160, 176, 192); font-style: italic;">// $mode ='bike';</span><br><span style="color: rgb(160, 176, 192); font-style: italic;">7<wbr></wbr></span><span style="color: rgb(91, 54, 116); font-style: italic;">$mode</span><span><wbr></wbr></span><span style="color: rgb(32, 32, 32);">=</span><span><wbr></wbr></span><span style="color: rgb(76, 143, 47); font-weight: bold;">end</span>(<span style="color: rgb(91, 54, 116); font-style: italic;">$transport</span>);<wbr><wbr><span><wbr></wbr></span><span style="color: rgb(160, 176, 192); font-style: italic;">// $mode ='plane';</span><br><span style="color: rgb(160, 176, 192); font-style: italic;">8<wbr></wbr></span><span style="color: rgb(16, 96, 160);">?&gt;</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></div><br><br><br>current(PHP 3, PHP 4, PHP 5)<br>current -- 返回数组中的当前单元<br>说明<br><br>mixed current ( array &amp;array )<br><br>每个数组中都有一个内部的指针指向它“当前的”单元,初始指向插入到数组中的第一个单元。<br>current() 函数返回当前被内部指针指向的数组单元的值,并不移动指针。如果内部指针指向超出了单元列表的末端,current()返回 FALSE。<br><br>警告<br>如果数组包含有空的单元(0 或者 "",空字符串)则本函数在碰到这个单元时也返回 FALSE。这使得用 current()不可能判断是否到了此数组列表的末端。要正确遍历可能含有空单元的数组,用 each() 函数。<br><br>例 1. current() 及相关函数的用法示例<br><div style="font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(240, 240, 240); font-family: Consolas, 'Lucida Console', 'Courier new'; color: rgb(32, 32, 32);"><span style="color: rgb(160, 176, 192); font-style: italic;">1<wbr></wbr></span><span style="color: rgb(16, 96, 160);">&lt;?php</span><br><span style="color: rgb(160, 176, 192); font-style: italic;">2<wbr></wbr></span><span style="color: rgb(91, 54, 116); font-style: italic;">$transport</span><span><wbr></wbr></span><span style="color: rgb(32, 32, 32);">=</span><span><wbr></wbr></span><span style="color: rgb(0, 112, 32); font-weight: bold;">array</span>(<span style="color: rgb(64, 112, 160);">'foot'</span><span style="color: rgb(32, 32, 32);">,</span><span><wbr></wbr></span><span style="color: rgb(64, 112, 160);">'bike'</span><span style="color: rgb(32, 32, 32);">,</span><span><wbr></wbr></span><span style="color: rgb(64, 112, 160);">'car'</span><span style="color: rgb(32, 32, 32);">,</span><span><wbr></wbr></span><span style="color: rgb(64, 112, 160);">'plane'</span>);<br><span style="color: rgb(160, 176, 192); font-style: italic;">3<wbr></wbr></span><span style="color: rgb(91, 54, 116); font-style: italic;">$mode</span><span><wbr></wbr></span><span style="color: rgb(32, 32, 32);">=</span><span><wbr></wbr></span><span style="color: rgb(76, 143, 47); font-weight: bold;">current</span>(<span style="color: rgb(91, 54, 116); font-style: italic;">$transport</span>);<span><wbr></wbr></span><span style="color: rgb(160, 176, 192); font-style: italic;">//$mode = 'foot';</span><br><span style="color: rgb(160, 176, 192); font-style: italic;">4<wbr></wbr></span><span style="color: rgb(91, 54, 116); font-style: italic;">$mode</span><span><wbr></wbr></span><span style="color: rgb(32, 32, 32);">=</span><span><wbr></wbr></span><span style="color: rgb(76, 143, 47); font-weight: bold;">next</span>(<span style="color: rgb(91, 54, 116); font-style: italic;">$transport</span>);<wbr><wbr><span style="color: rgb(160, 176, 192); font-style: italic;">// $mode ='bike';</span><br><span style="color: rgb(248, 16, 176);">5<wbr></wbr></span><span style="color: rgb(91, 54, 116); font-style: italic;">$mode</span><span><wbr></wbr></span><span style="color: rgb(32, 32, 32);">=</span><span><wbr></wbr></span><span style="color: rgb(76, 143, 47); font-weight: bold;">current</span>(<span style="color: rgb(91, 54, 116); font-style: italic;">$transport</span>);<span><wbr></wbr></span><span style="color: rgb(160, 176, 192); font-style: italic;">//$mode = 'bike';</span><br><span style="color: rgb(160, 176, 192); font-style: italic;">6<wbr></wbr></span><span style="color: rgb(91, 54, 116); font-style: italic;">$mode</span><span><wbr></wbr></span><span style="color: rgb(32, 32, 32);">=</span><span><wbr></wbr></span><span style="color: rgb(76, 143, 47); font-weight: bold;">prev</span>(<span style="color: rgb(91, 54, 116); font-style: italic;">$transport</span>);<wbr><wbr><span style="color: rgb(160, 176, 192); font-style: italic;">// $mode ='foot';</span><br><span style="color: rgb(160, 176, 192); font-style: italic;">7<wbr></wbr></span><span style="color: rgb(91, 54, 116); font-style: italic;">$mode</span><span><wbr></wbr></span><span style="color: rgb(32, 32, 32);">=</span><span><wbr></wbr></span><span style="color: rgb(76, 143, 47); font-weight: bold;">end</span>(<span style="color: rgb(91, 54, 116); font-style: italic;">$transport</span>);<wbr><wbr><span><wbr></wbr></span><span style="color: rgb(160, 176, 192); font-style: italic;">// $mode ='plane';</span><br><span style="color: rgb(160, 176, 192); font-style: italic;">8<wbr></wbr></span><span style="color: rgb(91, 54, 116); font-style: italic;">$mode</span><span><wbr></wbr></span><span style="color: rgb(32, 32, 32);">=</span><span><wbr></wbr></span><span style="color: rgb(76, 143, 47); font-weight: bold;">current</span>(<span style="color: rgb(91, 54, 116); font-style: italic;">$transport</span>);<span><wbr></wbr></span><span style="color: rgb(160, 176, 192); font-style: italic;">//$mode = 'plane';</span><br><span style="color: rgb(160, 176, 192); font-style: italic;">9<wbr></wbr></span><span style="color: rgb(16, 96, 160);">?&gt;</span></wbr></wbr></wbr></wbr></wbr></wbr></div><br></wbr></wbr>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-359688-1-1.html 上篇帖子: php定时执行任务的几个方法 下篇帖子: PHP简单方法判断文件是否是图片 PHP best way to check if file is an image
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表