在php数组中获取下一个值 [英] Getting the next value in a php array

查看:462
本文介绍了在php数组中获取下一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我想获取PHP数组中的下一个值:

I want to get the next value in a PHP array for example:

$array = array('a', 'b', 'c', 'd', 'e', 'f');
$current_array_val = 'c';
//so I want to run a code to get the next value in the array and
$next_array_val = 'd';
//And also another code to get the previous value which will be
$prev_array_val = 'b';

请如何运行我的代码以实现这一目标

Please how do I run my code to achieve this

推荐答案

http:// php .net / manual / ro / function.array-search.php

$index = array_search($current_array_val, $array);
if($index !== false && $index > 0 ) $prev = $array[$index-1];
if($index !== false && $index < count($array)-1) $next = $array[$index+1];

这篇关于在php数组中获取下一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆