基于数组生成上一个/下一个项目按钮(php)
我想知道是否有人可以帮助我根据所有项目的数组生成上一个/下一个按钮。
这是我的基本数组:
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => ITEM 1
)
[1] => stdClass Object
(
[id] => 5
[name] => ITEM 2
)
[2] => stdClass Object
(
[id] => 6
[name] => ITEM 3
)
[3] => stdClass Object
(
[id] => 7
[name] => ITEM 4
)
)
我想做的是:
查看:ITEM 1 上一个按钮:项目 4 下一个按钮:项目 2
查看:项目 2 上一个按钮:项目 1 下一个按钮:项目 3
查看:项目 3 上一个按钮:项目 2 下一个按钮:第 4 项
等
我想我真的想根据我正在查看的项目将原始数组转换为另一个上一个/下一个数组..如果这有意义..?任何帮助将不胜感激!
I was wondering if anyone could help me generate previous/next buttons based on an array of all items.
This is my basic array:
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => ITEM 1
)
[1] => stdClass Object
(
[id] => 5
[name] => ITEM 2
)
[2] => stdClass Object
(
[id] => 6
[name] => ITEM 3
)
[3] => stdClass Object
(
[id] => 7
[name] => ITEM 4
)
)
What I'm trying to do is:
Viewing: ITEM 1
Previous Button: ITEM 4
Next Button: ITEM 2
Viewing: ITEM 2
Previous Button: ITEM 1
Next Button: ITEM 3
Viewing: ITEM 3
Previous Button: ITEM 2
Next Button: ITEM 4
etc
I guess I'm really trying to turn my original array into another array of prev/next based on which item I am viewing.. if that makes sense..? Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有很多方法可以做到这一点,但您可以使用 array_shift / array_push 循环遍历数组。这不使用您提到的确切数组,但它应该让您接近您的解决方案。
这将打印:
请记住,这不会进行成员资格测试,因此如果 $current 不在数组中,您将最终陷入无限循环。另外,我将添加免责声明,我确信可能有更好的方法来做到这一点,这只是一种方法。
There are many ways to do this, but you could use
array_shift
/array_push
to cycle through the array of things. This doesn't use the exact array you mentioned, but it should get you close to your solution.This prints:
Keep in mind that this does no membership test, so if $current isn't in the array, you'll end up in an infinite loop. Also, I'll add the disclaimer that I'm sure there are probably better ways to do this, this is just one approach.