gpt4 book ai didi

php - 如何在 PHP 中重新索引数组但索引从 1 开始?

转载 作者:IT老高 更新时间:2023-10-28 11:44:48 25 4
gpt4 key购买 nike

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

当前数组(编辑:数组实际上是这样的):

Array (

[2] => Object
(
[title] => Section
[linked] => 1
)

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

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

)

应该如何:

Array (

[1] => Object
(
[title] => Section
[linked] => 1
)

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

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

)

最佳答案

如果您想从零开始重新索引,只需执行以下操作:

$iZero = array_values($arr);

如果您需要从一个开始,请使用以下内容:

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

这里是所用函数的手册页:

关于php - 如何在 PHP 中重新索引数组但索引从 1 开始?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/591094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com