如何将 Arrays 内部指针设置为特定位置?PHP/XML [英] How to set an Arrays internal pointer to a specific position? PHP/XML

查看:23
本文介绍了如何将 Arrays 内部指针设置为特定位置?PHP/XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 XML 而不是数据库来构建一个小站点.

Im trying to build a little site using XML instead of a database.

我想构建一个下一个和上一个按钮,该按钮将与我显示的内容相关.

I would like to build a next and prev button which will work relative to the content I have displayed.

我找到了 php 函数 next() 和 prev() 以及 current() 但我不知道如何将指针设置为特定位置以便能够相对于当前页面导航.

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')

例如,如果我显示 page2 的内容,我怎么能告诉 php 我在 $list[1] 以便 next($list) 显示 page3?

eg if im displaying contents of page2 how could I tell php i am at $list[1] so that next($list) shows page3?

提前致谢

推荐答案

如果你的数组总是索引一致(例如,'page1' 总是在索引 '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',
);

如果要测试数组的值(而不是键),请使用 current():

If you want to test the values of the array (instead of the keys), use current():

while (current($List) !== $CurrentPage) next($List);

这篇关于如何将 Arrays 内部指针设置为特定位置?PHP/XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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