PHP数组next()和preV() [英] php arrays next() and prev()
问题描述
我有一个数组,看起来像这样,
I have an array that looks like this,
Array
(
[0] => 849710414
[1] => 849710726
[2] => 849710744
[3] => 849712728
[4] => 849713005
[5] => 849713439
[6] => 849714856
[7] => 849714924
[8] => 849716371
[9] => 849716441
[10] => 849717118
[11] => 849719043
[12] => 849719187
[13] => 849722865
[14] => 849723412
[15] => 849723777
[16] => 849725052
[17] => 849726294
[18] => 849726457
[19] => 849726902
[20] => 849728239
[21] => 849728372
[22] => 849728449
[23] => 849730353
[24] => 849733625
)
这些ID的,我需要添加到URL来浏览这个网站四周,ID是获取用户的搜索返回的结果,然后用户点击搜索结果并具有去到下一个选项搜索结果是看previous之一,现在我虽然我能做到这一点,做
These are ID's that I need to add to an URL to navigate around the site, the IDs are the results that get returned from a user's search, the user then clicks on a search result and has the option of going to the next search result are look at the previous one, now I though I would be able to do this, by doing
<a href="<?=next($array);?>">Next</a>
<a href="<?=prev($array);?>">Prev</a>
我似乎永远那么不承认我在看当前结果ID是什么工作,什么是下一个和previous?有谁现在我怎么能解决这个问题?
How ever I does not seem to recognise what the current result ID I am looking at is to work what is next and previous? Does anybody now how I can solve this?
推荐答案
下一个()
和 preV( )
处理,而你的脚本数组的内部指针正在处理中。
next()
and prev()
deal with the internal pointer of the array while your script is processing.
你需要做的是找出当前正在查看数组的索引( array_search()
),然后加1获得下一个值,减去1获得previous值。
What you need to do is find out which index of the array you are currently viewing, (array_search()
) then add 1 to get the next value, and subtract 1 to get the previous value.
// Find the index of the current item
$current_index = array_search($current_id, $array);
// Find the index of the next/prev items
$next = $current_index + 1;
$prev = $current_index - 1;
您的HTML:
<?php if ($prev > 0): ?>
<a href="<?= $array[$prev] ?>">Previous</a>
<?php endif; ?>
<?php if ($next < count($array)): ?>
<a href="<?= $array[$next] ?>">Next</a>
<?php endif; ?>
这篇关于PHP数组next()和preV()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!