douzhuo1858
2016-06-02 12:28
浏览 588
已采纳

获取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

图片转代码服务由CSDN问答提供 功能建议

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

  $ array = array('a','b','c','d','e','f'); 
 $ current_array_val ='c'; 
 //所以我想要 运行代码以获取数组中的下一个值并且
 $ next_array_val ='d'; 
 //还有另一个代码来获取前一个值
 $ prev_array_val ='b'; 
 <  / code>  
 
 

请如何运行我的代码来实现此目的

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • dongyu3712 2016-06-02 12:33
    已采纳

    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];
    
    点赞 评论
  • douqiong8412 2016-06-02 12:30

    use next() function:

    In addition: use current() or prev()

    $array = array('a', 'b', 'c', 'd', 'e', 'f');
    
    $current= current($array); // 'a'
    $nextVal = next($array); // 'b'
    $nextVal = next($array); // 'c'
    
    // ... 
    
    点赞 评论
  • dsfg3241 2016-06-02 12:33
    $array = array('a', 'b', 'c', 'd', 'e', 'f');
    
    $flipped_array = array_flip($array);
    
    $middle_letter = 'c'; //Select your middle letter here
    
    $index_of_middle_letter = $flipped_array[$middle_letter];
    
    $next_index = $index_of_middle_letter + 1;
    $prev_index = $index_of_middle_letter - 1;
    $next_item = $array[$next_index];
    $prev_item = $array[$prev_index];
    

    array_search() is slower than doing an array_flip() when it comes to dealing with large arrays. The method I have described above is far more scalable.

    点赞 评论
  • duansengcha9114 2016-06-02 12:35

    Use array_search, and increment/decrement for next/prev item in array.

    $array = array('a', 'b', 'c', 'd', 'e', 'f');
    $current_array_val = array_search('c', $array);
    //so I want to run a code to get the next value in the array and
    $next_array_val = $array[$current_array_val+1];
    //And also another code to get the previous value which will be
    $prev_array_val = $array[$current_array_val-1];
    
    
    echo $next_array_val; // print d
    echo $prev_array_val; // print b
    
    点赞 评论
  • duandou8457 2016-06-02 12:44

    Take a look at this code sample for a better understanding of the object-oriented way of navigating through arrays:

    $array = array('a', 'b', 'c', 'd', 'e', 'f');
    
    $pointer = 'c';
    
    // Create new iterator
    $arrayobject = new ArrayObject($array);
    $iterator = $arrayobject->getIterator();
    
    $iterator->seek($pointer);     //set position
    
    // Go to the next value
    $iterator->next();             // move iterator 
    
    // Assign next value to a variable
    $nextValue = $iterator->current();
    
    点赞 评论

相关推荐 更多相似问题