你如何重新索引在PHP中的数组?

编程入门 行业动态 更新时间:2024-10-11 19:25:16
本文介绍了你如何重新索引在PHP中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下的数组,我想重新索引,以便键被逆转(最好从1开始):

当前阵列(修改的数组实际上看起来像这样):

阵列([2] =>目的    (        [标题] =>部分        [链接] => 1    )[1] =>目的    (        [标题] =>次节        [链接] => 1    )[0] =>目的    (        [标题] =>分次节        [链接] =>    ))

就应该是这样:

阵列([1] =>目的    (        [标题] =>部分        [链接] => 1    )[2] =>目的    (        [标题] =>次节        [链接] => 1    )[3] =>目的    (        [标题] =>分次节        [链接] =>    ))

解决方案

如果你想重新开始指数为零,只需做到以下几点:

$ iZero = array_values​​($ ARR);

如果你需要它的一个开始,然后使用下列内容:

$卓智= array_combine(范围(1,计数($ ARR)),array_values​​($ ARR));

下面是使用的函数的手册页:

  • array_values​​()
  • array_combine()
  • 范围()

I have the following array, which I would like to reindex so the keys are reversed (ideally starting at 1):

Current array (edit: the array actually looks like this):

Array ( [2] => Object ( [title] => Section [linked] => 1 ) [1] => Object ( [title] => Sub-Section [linked] => 1 ) [0] => Object ( [title] => Sub-Sub-Section [linked] => ) )

How it should be:

Array ( [1] => Object ( [title] => Section [linked] => 1 ) [2] => Object ( [title] => Sub-Section [linked] => 1 ) [3] => Object ( [title] => Sub-Sub-Section [linked] => ) )

解决方案

If you want to re-index starting to zero, simply do the following:

$iZero = array_values($arr);

If you need it to start at one, then use the following:

$iOne = array_combine(range(1, count($arr)), array_values($arr));

Here are the manual pages for the functions used:

  • array_values()
  • array_combine()
  • range()

更多推荐

你如何重新索引在PHP中的数组?

本文发布于:2023-11-28 16:18:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643101.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   索引   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!