多维混合关联/数字数组移位

发布于 2024-07-21 09:57:06 字数 1152 浏览 2 评论 0原文

我有一个问题需要尽快解决。 如果我有时间重写整个脚本我会的,但这就是程序员的生活,对吗? 无论如何,我已经接管了一个项目,并且我有一个多维混合关联/数字数组,如下所示:

Array
(
    [item1] => Array
        (
            [dataset] => Array()
            [3] => Array()
            [7] => Array()
        )
    [item2] => Array
        (
            [dataset] => Array()
            [4] => Array()
            [19] => Array()
            [2] => Array()
        )
)

我需要做的是移动每个 itemX 中的 dataset 索引> 索引是导致此结果的最后一个索引:

Array
(
    [item1] => Array
        (
            [3] => Array()
            [7] => Array()
            [dataset] => Array()
        )
    [item2] => Array
        (
            [4] => Array()
            [19] => Array()
            [2] => Array()
            [dataset] => Array()
        )
)

可能有助于实现此目的的一些事情是我知道数据集索引将始终是 itemX 索引中的第一个索引,键始终是“数据集”,其他索引始终是数字索引。 有没有办法在 php 中做到这一点? 事实上,它是一个混合数组,这让我很困惑。 我不能让数字索引重置并从 0 开始。它们的顺序是否发生变化并不重要,只是它们都位于“数据集”索引之前。 也许这只是那些日子之一......:\ 非常感谢任何建议或评论。

i have an issue i need to fix sooner than later. if i had the time to rewrite the entire script i would, but such is the life of a programmer, right? anywho, i've taken over a project and i have a multidimensional mixed associative/numeric array like so:

Array
(
    [item1] => Array
        (
            [dataset] => Array()
            [3] => Array()
            [7] => Array()
        )
    [item2] => Array
        (
            [dataset] => Array()
            [4] => Array()
            [19] => Array()
            [2] => Array()
        )
)

what i need to do is shift the dataset index in each of the itemX indexes to be the last index to result this:

Array
(
    [item1] => Array
        (
            [3] => Array()
            [7] => Array()
            [dataset] => Array()
        )
    [item2] => Array
        (
            [4] => Array()
            [19] => Array()
            [2] => Array()
            [dataset] => Array()
        )
)

a few things that may help make this happen is that i know that the dataset index will always be the first index in the itemX index and the key will always be 'dataset' and the others will all always be numeric indexes. is there anyway to do this in php? the fact that it's a mixed array is throwing me. i can't have the numeric indexes getting reset and starting at 0. it doesn't matter if they're order is shifted, only that they all come before the 'dataset' index. maybe it's just one of those days.... :\ any suggestions or comments are greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

苏璃陌 2024-07-28 09:57:06

像这样循环所有元素:

foreach ($all_items as $key =>$items) {
   $dataset = $items['dataset'];
   unset($all_items[$key]['dataset']); // Removing it (from the top)
   $all_items[$key]['dataset'] = $dataset; // Adding it again (at the bottom)
}

取消设置“dataset”元素并再次添加它将导致该元素添加到底部。

重要的是直接修改原始数组,而不是 foreach 中的 $items,因为这些更改不会影响原始数组。

Loop though all elements like this:

foreach ($all_items as $key =>$items) {
   $dataset = $items['dataset'];
   unset($all_items[$key]['dataset']); // Removing it (from the top)
   $all_items[$key]['dataset'] = $dataset; // Adding it again (at the bottom)
}

Unsetting the 'dataset' element and adding it again will cause the element to be added at the bottom.

It's important that you modify the original array directly, not the $items from the foreach, because those changes will not affect the original array.

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