基于数组生成上一个/下一个项目按钮(php)

发布于 2024-12-14 14:56:14 字数 737 浏览 0 评论 0原文

我想知道是否有人可以帮助我根据所有项目的数组生成上一个/下一个按钮。

这是我的基本数组:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看春风乍起 2024-12-21 14:56:14

有很多方法可以做到这一点,但您可以使用 array_shift / array_push 循环遍历数组。这不使用您提到的确切数组,但它应该让您接近您的解决方案。

<?php

function next_and_prev($current, $a) {
    while (true) {
        /* Test for item at 2nd position in the array, so if we hit a match,
           we can just grab the first and third items as our prev/next results.

           Since we are mutating the array on each iteration of the loop, the
           value of $a[1] will change each time */
        if ($current == $a[1]) {
            print "Prev: " . $a[0] . "\n";
            print "Next: " . $a[2] . "\n";
            return;
        }
        array_push($a, array_shift($a));
    }
}

print "with current of 1\n";
next_and_prev(1, array(1, 2, 3));

print "with current of 2\n";
next_and_prev(2, array(1, 2, 3));

这将打印:

with current of 1
Prev: 3
Next: 2
with current of 2
Prev: 1
Next: 3

请记住,这不会进行成员资格测试,因此如果 $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.

<?php

function next_and_prev($current, $a) {
    while (true) {
        /* Test for item at 2nd position in the array, so if we hit a match,
           we can just grab the first and third items as our prev/next results.

           Since we are mutating the array on each iteration of the loop, the
           value of $a[1] will change each time */
        if ($current == $a[1]) {
            print "Prev: " . $a[0] . "\n";
            print "Next: " . $a[2] . "\n";
            return;
        }
        array_push($a, array_shift($a));
    }
}

print "with current of 1\n";
next_and_prev(1, array(1, 2, 3));

print "with current of 2\n";
next_and_prev(2, array(1, 2, 3));

This prints:

with current of 1
Prev: 3
Next: 2
with current of 2
Prev: 1
Next: 3

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文