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

mel*_*yal 139 php arrays indexing

我有以下数组,我想重新索引,所以键是相反的(理想情况下从1开始):

当前数组(编辑:数组实际上看起来像这样):

Array (

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

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

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

)
Run Code Online (Sandbox Code Playgroud)

怎么样:

Array (

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

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

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

)
Run Code Online (Sandbox Code Playgroud)

And*_*ore 403

如果要将索引重新开始归零,只需执行以下操作:

$iZero = array_values($arr);
Run Code Online (Sandbox Code Playgroud)

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

$iOne = array_combine(range(1, count($arr)), array_values($arr));
Run Code Online (Sandbox Code Playgroud)

以下是所用功能的手册页:

  • @MaxHartshorn 这不是这个问题要问的! (2认同)

San*_*dra 54

这是最好的方法:

# Array
$array = array('tomato', '', 'apple', 'melon', 'cherry', '', '', 'banana');
Run Code Online (Sandbox Code Playgroud)

返回

Array
(
    [0] => tomato
    [1] => 
    [2] => apple
    [3] => melon
    [4] => cherry
    [5] => 
    [6] => 
    [7] => banana
)
Run Code Online (Sandbox Code Playgroud)

通过做这个

$array = array_values(array_filter($array));
Run Code Online (Sandbox Code Playgroud)

你明白了

Array
(
    [0] => tomato
    [1] => apple
    [2] => melon
    [3] => cherry
    [4] => banana
)
Run Code Online (Sandbox Code Playgroud)

说明

array_values() :以数字方式返回输入数组和索引的值.

array_filter():使用用户定义的函数过滤数组的元素(UDF 如果未提供,则将删除输入表中值为FALSE的所有条目.)

  • 如果你不关心订单,你也可以只排序($ array); (3认同)

ima*_*iro 11

我刚刚发现你也可以做一个

array_splice($ar, 0, 0);
Run Code Online (Sandbox Code Playgroud)

这会重新建立索引,因此您不会得到原始数组的副本.


Gum*_*mbo 9

为什么重新索引?只需在索引中添加1:

foreach ($array as $key => $val) {
    echo $key + 1, '<br>';
}
Run Code Online (Sandbox Code Playgroud)

编辑    问题澄清之后:您可以使用array_values从0开始重置索引.如果您只是希望打印元素从1开始,则可以使用上面的算法.


Pet*_*ley 6

好吧,我想,无论你的最终目标是什么,你实际上都不需要将数组修改为基于1而不是基于0,但是可以像Gumbo发布的那样在迭代时间处理它.

但是,要回答您的问题,此函数应将任何数组转换为基于1的版本

function convertToOneBased( $arr )
{
    return array_combine( range( 1, count( $arr ) ), array_values( $arr ) );
}
Run Code Online (Sandbox Code Playgroud)

编辑

如果您需要,这是一个更可重用/灵活的功能

$arr = array( 'a', 'b', 'c' );

echo '<pre>';
print_r( reIndexArray( $arr ) );
print_r( reIndexArray( $arr, 1 ) );
print_r( reIndexArray( $arr, 2 ) );
print_r( reIndexArray( $arr, 10 ) );
print_r( reIndexArray( $arr, -10 ) );
echo '</pre>';

function reIndexArray( $arr, $startAt=0 )
{
    return ( 0 == $startAt )
        ? array_values( $arr )
        : array_combine( range( $startAt, count( $arr ) + ( $startAt - 1 ) ), array_values( $arr ) );
}
Run Code Online (Sandbox Code Playgroud)


Fat*_*ror 6

我能想到的最快的方法

array_unshift($arr, null);
unset($arr[0]);
print_r($arr);
Run Code Online (Sandbox Code Playgroud)

如果你只是想重新索引数组(从零开始)并且你有 PHP +7.3,你可以这样做

array_unshift($arr);
Run Code Online (Sandbox Code Playgroud)

我相信array_unshift比前者更好,array_values因为前者不创建数组的副本。

变更日志

版本 描述
7.3.0 现在可以仅使用一个参数来调用该函数。以前,至少需要两个参数。

-- https://www.php.net/manual/en/function.array-unshift.php#refsect1-function.array-unshift-changelog


Gre*_*reg 5

这将做你想要的:

<?php

$array = array(2 => 'a', 1 => 'b', 0 => 'c');

array_unshift($array, false); // Add to the start of the array
$array = array_values($array); // Re-number

// Remove the first index so we start at 1
$array = array_slice($array, 1, count($array), true);

print_r($array); // Array ( [1] => a [2] => b [3] => c ) 

?>
Run Code Online (Sandbox Code Playgroud)


Mic*_*sch 5

您可能要考虑为什么完全要使用基于1的数组。从零开始的数组(使用非关联数组时)是非常标准的,如果要输出到UI,大多数将通过在输出到UI时增加整数来处理该解决方案。

在考虑基于1的数组索引器时,请考虑应用程序和使用的代码中的一致性。

  • 这与业务层和表示层之间的分离直接相关。如果您正在修改逻辑中的代码以适应演示,那么您所做的事情就很糟糕。例如,如果您对控制器执行此操作,则突然将您的控制器绑定到特定的视图渲染器,而不是为其可能使用的任何视图渲染器准备数据(php,json,xml,rss等)。 (2认同)

Ion*_*şca 5

更优雅的解决方案:

$list = array_combine(range(1, count($list)), array_values($list));

  • 你称之为优雅吗? (6认同)

Nig*_*ton 5

您可以重新索引数组,以便新数组以索引1开头,就像这样;

$arr = array(
  '2' => 'red',
  '1' => 'green',
  '0' => 'blue',
);

$arr1 = array_values($arr);   // Reindex the array starting from 0.
array_unshift($arr1, '');     // Prepend a dummy element to the start of the array.
unset($arr1[0]);              // Kill the dummy element.

print_r($arr);
print_r($arr1);
Run Code Online (Sandbox Code Playgroud)

以上的输出是;

Array
(
    [2] => red
    [1] => green
    [0] => blue
)
Array
(
    [1] => red
    [2] => green
    [3] => blue
)
Run Code Online (Sandbox Code Playgroud)