- 浏览: 26930 次
- 性别:
- 来自: 广州
最新评论
php:获取数组第一个值
php 获取数组 第一个值 <wbr><br><br><br>
reset (PHP 3, PHP 4, PHP 5)<br>
reset -- 将数组的内部指针指向第一个单元<br>
说明<br>
mixed reset ( array &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);"><?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);">'step
one'</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);">"<br
/></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;">// "step
one"</span><br><span style="color: rgb(248, 16, 176);">05<wbr></wbr></span><span style="color: rgb(160, 176, 192); font-style: italic;">// skip two
steps</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);">"<br
/></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;">// "step
three"</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);">"<br
/></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;">// "step
one"</span><br><span style="color: rgb(160, 176, 192); font-style: italic;">12<wbr></wbr></span><span style="color: rgb(16, 96, 160);">?></span>
</div>
<br><br><br>
next(PHP 3, PHP 4, PHP 5)<br>
next --<wbr> 将数组中的内部指针向前移动一位<br>
说明<br>
mixed next ( array &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);"><?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);">?></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 &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);"><?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);">?></span></wbr></wbr></wbr></wbr></wbr></wbr>
</div>
<br></wbr></wbr>
相关推荐
以下是对使用php实现获取数组第一个值的方法进行了详细的分析介绍,需要的朋友可以过来参考下
在开发中经常遇到这样问题,获取数组第一个元素的值,如果是数字索引那还好,直接$array[0],如果键名是字符串,你又未知这个字符串呢?用current()函数就可以做到
本文实例讲述了php获取数组元素中头一个数组元素值的实现方法。分享给大家供大家参考。具体如下: 在php的内置函数中,获取数组元素值的函数主要有 reset next current prev end 这几个函数. reset (PHP 3, PHP 4, ...
php数组1,这里是php技术中的数组部分的第一个png图像
PHP还是比较常用的,于是我研究了一下PHP二维数组,下面通过本文给大家介绍PHP获取二维数组中某一列的值集合,对php数组二维数组的值相关知识感兴趣的朋友一起学习吧
array_values() 定义和用法 array_keys() 函数返回包含数组中所有键名的一个新数组。 如果提供了第二个参数,则只返回键值为该值的键名。 如果 strict 参数指定为 true,则 PHP 会使用全等比较 (===) 来严格检查键值...
Reset() 移动到第一个单元 获得第一个元素的值。 如果移动不成功,返回false。 参数都为需要操作的数组,并且是引用传递。 获得指针指向的元素的信息: Key();//获得当前数组指针指向的元素下标 Current();//获得...
对于一个多维数组(以二维数组为例),经常需要获取到其中的一列,比如有几条用户数据,是一个二维数组,现在需要获取到这几个用户的名字,可以有多种方法实现: $arr = array( array( 'id'=>'101', 'name'=>'qu...
本文实例讲述了php求数组全排列,元素所有组合的方法。分享给大家供大家参考,具体如下: <?php $source = array('pll','... //输出第一种组合 while (true) { $y = $x--; //相邻的两个元素 if ($source[$x] <
array_keys() 定义和用法 array_keys() 函数返回包含数组中所有键名的一个新数组。 如果提供了第二个参数,则只返回键值为该值的键名。 如果 strict 参数指定为 true,则 PHP 会使用全等比较 (===) 来严格检查键值的...
array_keys() 函数返回包含数组中所有键名的一个新数组。如果提供了第二个参数,则只返回键值为该值的键名
表达式1:表达式2; 条件是一个boolean类型的数据,如果条件结果为true,则执行表达式1的内容,并将表达式1的结果作为整体表达式的结果。如果条件为false,则执行表达式2的内容,并将表达式2的结果作为整体表达式的...
2.任务:从数组获取第一个值,并检查它是否不是空字符串 例如,如果您处理请求并想处理get / post参数。 可以从具有许多值的某个过滤器接收它,例如无线电组或类似的东西。 并且您想要获得许多价值中的第一价值。 ...
1.运行环境 本书7~10章所附代码的运行环境 Windows家族,.NET Framework 2.0及以上版本 IIS 5.0及以上版本 本书14~16章所附代码的运行环境 Windows家族,Apache 2.0及以上版本 PHP 5.0及以上版本 本书17~18...
回调函数拥有两个参数:第一个为对象的成员或数组的索引, 第二个为对应变量或内容. 如果需要退出 each 循环可使回调函数返回 false, 其它返回值将被忽略. each遍历,相信都不陌生,在平常的事件处理中,是for循环...
//遍历第二个数组,如果键名不存在与第一个数组,将数组元素增加到第一个数组 foreach($b as $key=>$value){ if(!array_key_exists($key, $a)){ $a[$key]=$value; } } //计算键名相同的数组元素的和,并且替换原...