你如何重新索引在PHP中的数组? [英] How do you reindex an array in PHP?

查看:187
本文介绍了你如何重新索引在PHP中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

就应该是这样:

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


解决方案

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

  $ iZero = array_values​​($ ARR);

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

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

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

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:

这篇关于你如何重新索引在PHP中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆