如何在php中的数组中添加缺失的索引?

时间:2021-08-12 03:19:32

Example:

I have and array like this:

我有和像这样的数组:

Array( [0] => Apple [2] => Orange [5] => Pear [8] => Pear )

数组([0] => Apple [2] =>橙[5] =>梨[8] =>梨)

there are a function to complete the missing indexes: 1,3,4,6,7????

有一个功能来完成缺失的索引:1,3,4,6,7 ????

4 个解决方案

#1


This should be faster for larger arrays. For smaller arrays any method will do.

对于较大的阵列,这应该更快。对于较小的阵列,任何方法都可以。

$existingKeys = array_keys($array);

//you can use any value instead of null
$newKeys = array_fill_keys(range(min($existingKeys), max($existingKeys)), null);
$array += $newKeys;

//optional, probably not needed
ksort($array);

#2


you could try a for() from the lowest index to the highest and complete if it's empty

您可以尝试从最低索引到最高索引的for(),如果它是空的则完成

for($i = 0 ;$i <= 8 ; $i++) 
{
//if it's not set
if(!isset($array[$i]))
{
//set to empty
$array[$i] = "";
}

}

Additionally you could count first the number of elements on the array and wrap it in a function

此外,您可以首先计算阵列上的元素数量并将其包装在函数中

 function completeIndexes($array)
    {

    $total = count($array);
     for($i = 0 ;$i < $total ; $i++) 

        {
        //if it's not set
        if(!isset($array[$i]))
        {
        //set to empty
        $array[$i] = "";
        }

        }
return $array; 
    }

#3


for($i=0;i<count($array);++$i){
    $array[$i] = isset($array[$i])? $array[$i] : '';
}

It just fills the missing keys with an empty string, though. Not sure if this suits you.

但它只是用空字符串填充缺少的键。不确定这是否适合你。

Edit

Just noticed Perr0_hunter wrote pretty much the same thing before I did :P

刚刚注意到Perr0_hunter在我做之前写了几乎相同的东西:P

#4


If what you're trying to do is reorder the array so you get

如果你要做的是重新排序数组,那么你得到

Array( [0] => Apple [1] => Orange [2] => Pear [3] => Pear )

Just create a new Array and copy the values into it. It will allocate new indexes sequentially

只需创建一个新数组并将值复制到其中。它将按顺序分配新索引

i.e.

$new_array = array();
for( $value in $old_array )
  $new_array[] = $value;

#1


This should be faster for larger arrays. For smaller arrays any method will do.

对于较大的阵列,这应该更快。对于较小的阵列,任何方法都可以。

$existingKeys = array_keys($array);

//you can use any value instead of null
$newKeys = array_fill_keys(range(min($existingKeys), max($existingKeys)), null);
$array += $newKeys;

//optional, probably not needed
ksort($array);

#2


you could try a for() from the lowest index to the highest and complete if it's empty

您可以尝试从最低索引到最高索引的for(),如果它是空的则完成

for($i = 0 ;$i <= 8 ; $i++) 
{
//if it's not set
if(!isset($array[$i]))
{
//set to empty
$array[$i] = "";
}

}

Additionally you could count first the number of elements on the array and wrap it in a function

此外,您可以首先计算阵列上的元素数量并将其包装在函数中

 function completeIndexes($array)
    {

    $total = count($array);
     for($i = 0 ;$i < $total ; $i++) 

        {
        //if it's not set
        if(!isset($array[$i]))
        {
        //set to empty
        $array[$i] = "";
        }

        }
return $array; 
    }

#3


for($i=0;i<count($array);++$i){
    $array[$i] = isset($array[$i])? $array[$i] : '';
}

It just fills the missing keys with an empty string, though. Not sure if this suits you.

但它只是用空字符串填充缺少的键。不确定这是否适合你。

Edit

Just noticed Perr0_hunter wrote pretty much the same thing before I did :P

刚刚注意到Perr0_hunter在我做之前写了几乎相同的东西:P

#4


If what you're trying to do is reorder the array so you get

如果你要做的是重新排序数组,那么你得到

Array( [0] => Apple [1] => Orange [2] => Pear [3] => Pear )

Just create a new Array and copy the values into it. It will allocate new indexes sequentially

只需创建一个新数组并将值复制到其中。它将按顺序分配新索引

i.e.

$new_array = array();
for( $value in $old_array )
  $new_array[] = $value;