数组重新排序并替换帮助。 (新数组的键和值)

发布于 2024-10-10 06:29:21 字数 1292 浏览 0 评论 0原文

在这种情况下请帮助我。我有一个数组,我想创建一个新的数组,就像

array[0][id] = 1
array[0][parent_id] = null
array[0][order_id] = 1
array[1][id] = 2
array[1][parent_id] = null
array[1][order_id] = 2
etc.

我当前的数组是这样的:

Array
(
[list] => Array // if the (array's name = `list`) the array[#][parent_id] = null
    (
        [0] => 1 // the key of the array (`[0]` in this case) is the array[#][order_id]
        [1] => 2 // the value of the array (`2` in this case) is the array[#][id]
        [2] => 5
    )

[list_2] => Array // else the array[#][parent_id] = the number value after the _
    (
        [0] => 3
        [1] => 4
    )

[list_5] => Array
    (
        [0] => 6
    )
)

基于 Habax 脚本的良好解决方案:

$new = array();
$n=0;
foreach($old as $key=>$item){
    for ($i = 1; $i <= count($item); $i++) {
        if($key=='list')$new[$n]['parent_id']=null;
        else {
          $tmp = explode('_', $key);
          $new[$n]['parent_id']=$tmp[1];
        }
        $new[$n]['order_id']=$i;
        $new[$n]['id']=$item[$i-1];
        $n++;
    }
}
}

很好地安排嵌套排序,例如: http://www.b-hind.eu/jquery/

Please help me in this case. I've got an array, and I'd like to make a new one, like

array[0][id] = 1
array[0][parent_id] = null
array[0][order_id] = 1
array[1][id] = 2
array[1][parent_id] = null
array[1][order_id] = 2
etc.

My current array is like this:

Array
(
[list] => Array // if the (array's name = `list`) the array[#][parent_id] = null
    (
        [0] => 1 // the key of the array (`[0]` in this case) is the array[#][order_id]
        [1] => 2 // the value of the array (`2` in this case) is the array[#][id]
        [2] => 5
    )

[list_2] => Array // else the array[#][parent_id] = the number value after the _
    (
        [0] => 3
        [1] => 4
    )

[list_5] => Array
    (
        [0] => 6
    )
)

A good solution based on Habax's script:

$new = array();
$n=0;
foreach($old as $key=>$item){
    for ($i = 1; $i <= count($item); $i++) {
        if($key=='list')$new[$n]['parent_id']=null;
        else {
          $tmp = explode('_', $key);
          $new[$n]['parent_id']=$tmp[1];
        }
        $new[$n]['order_id']=$i;
        $new[$n]['id']=$item[$i-1];
        $n++;
    }
}
}

Good to arrange nested sortables, like: http://www.b-hind.eu/jquery/

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

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

发布评论

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

评论(1

怀念你的温柔 2024-10-17 06:29:21

我不确定你的台词,

[0] => 1 // the key of the array (`[0]` in this case) is the array[#][order_id]

我认为这是价值,而不是关键:

$new = array();
$n=0;
foreach($old as $key=>$item){
  if($key=='list')$new[$n]['parent_id']=null;
  else {
    $tmp = explode('_', $key);
    $new[$n]['parent_id']=$tmp[1];
  }
  $new[$n]['order_id']=$item[0];
  $new[$n]['id']=$item[1];
  $n++;
}

I'm not sur to understand your line

[0] => 1 // the key of the array (`[0]` in this case) is the array[#][order_id]

I will suppose it's the value, not the key:

$new = array();
$n=0;
foreach($old as $key=>$item){
  if($key=='list')$new[$n]['parent_id']=null;
  else {
    $tmp = explode('_', $key);
    $new[$n]['parent_id']=$tmp[1];
  }
  $new[$n]['order_id']=$item[0];
  $new[$n]['id']=$item[1];
  $n++;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文