来自多维数组的 PHP 度假村键

发布于 2024-12-21 10:39:54 字数 1274 浏览 0 评论 0原文

我有一个多维数组,其索引/键(而不是值)如下所示:

这是提交的数组的外观

  [param] => Array
            (
                [3] => groupedlista
                [0] => groupedlistb
                [2] => groupedlistc
            )

        [f_name] => Array
            (
                [3] => grouplistaa
                [0] => grouplistbb
                [2] => grouplistcc
            )

        [f_label] => Array
            (
                [3] => grouplistL3
                [0] => grouplistL0
                [2] => grouplistL2
            )

这是顺序的外观

0,2,3

我想要的结果

  [param] => Array
            (
                [0] => groupedlistb
                [1] => groupedlistc
                [2] => groupedlista
            )

        [f_name] => Array
            (
                [0] => grouplistbb
                [1] => grouplistcc
                [2] => grouplistaa
            )

        [f_label] => Array
            (
                [0] => grouplistL0
                [1] => grouplistL2
                [2] => grouplistL3
            )

就是这样

PS:我使用 jquery 排序/添加/删除功能表单,我更喜欢基于 php 进行最终排序。索引数组[$i]需要在表单中声明。

i have a multidimensional array whose index/keys (not the values) are like this:

this is how the submitted array looks

  [param] => Array
            (
                [3] => groupedlista
                [0] => groupedlistb
                [2] => groupedlistc
            )

        [f_name] => Array
            (
                [3] => grouplistaa
                [0] => grouplistbb
                [2] => grouplistcc
            )

        [f_label] => Array
            (
                [3] => grouplistL3
                [0] => grouplistL0
                [2] => grouplistL2
            )

this is how the order looks

0,2,3

i want that Result

  [param] => Array
            (
                [0] => groupedlistb
                [1] => groupedlistc
                [2] => groupedlista
            )

        [f_name] => Array
            (
                [0] => grouplistbb
                [1] => grouplistcc
                [2] => grouplistaa
            )

        [f_label] => Array
            (
                [0] => grouplistL0
                [1] => grouplistL2
                [2] => grouplistL3
            )

that's it

PS: i use a jquery sort / add / delete feature in the form and i prefer to do the final sorting php-based. the index array [$i] is required to be declared at the form.

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

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

发布评论

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

评论(1

无名指的心愿 2024-12-28 10:39:54
$order = '0,2,3';

$out = array(); // This will hold the sorted values
$order = explode(',',$order); // Turn the order into an array
foreach ($multiDimArray as $key => $subArray) { // Loop outer array
  foreach ($order as $pos) { // Loop order array
    if (isset($subArray[$pos])) { // Make sure the key exists
      $out[$key][] = $subArray[$pos]; // Put the correct value in the correct place
    }
  }
}

print_r($out);
$order = '0,2,3';

$out = array(); // This will hold the sorted values
$order = explode(',',$order); // Turn the order into an array
foreach ($multiDimArray as $key => $subArray) { // Loop outer array
  foreach ($order as $pos) { // Loop order array
    if (isset($subArray[$pos])) { // Make sure the key exists
      $out[$key][] = $subArray[$pos]; // Put the correct value in the correct place
    }
  }
}

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