如何设置一个数组内部指针到一个特定的位置? PHP / XML [英] How to set an Arrays internal pointer to a specific position? PHP/XML
问题描述
我试着去建立使用XML,而不是数据库的小网站。
Im trying to build a little site using XML instead of a database.
我想建立一个和$ P $光伏按钮,将工作,相对于我所显示的内容。
I would like to build a next and prev button which will work relative to the content I have displayed.
我发现下一个PHP函数()和preV()和电流(),但我不知道如何将指针设置为特定位置,能够浏览相对于当前页面。
I found the php function next() and prev() as well as current() but I do not know how to set the pointer to a specific position to be able to navigate relative to the current page.
$list=array('page1','page2','page3')
例如,如果第2页的IM内容显示怎么可能我告诉PHP我在$列表[1]因此,明年($表)显示第3页?
eg if im displaying contents of page2 how could I tell php i am at $list[1] so that next($list) shows page3?
在此先感谢
推荐答案
如果您的数组总是一致的索引(例如,第1页总是在指数'0'),这是相当简单:
If your array is always indexed consistently (eg. 'page1' is always at index '0'), it's fairly simple:
$List = array('page1', 'page2', 'page3', 'page4', 'page5');
$CurrentPage = 3; // 'page4'
while (key($List) !== $CurrentPage) next($List); // Advance until there's a match
我个人不依赖于自动索引,因为总有一个机会,将自动索引可能会改变。你应该考虑显性地定义键:
I personally don't rely on automatic indexing because there's always a chance that the automatic index might change. You should consider explicitly defining the keys:
$List = array(
'1' => 'page1',
'2' => 'page2',
'3' => 'page3',
);
编辑:如果您想要测试的阵列(而非键)的值,使用 电流()
:
If you want to test the values of the array (instead of the keys), use current()
:
while (current($List) !== $CurrentPage) next($List);
这篇关于如何设置一个数组内部指针到一个特定的位置? PHP / XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!