使用array_fill php消除数组索引

时间:2022-06-21 22:29:43

I went through the post - php - array_fill negative indices

我经历了post-php-array_fill负面指数

PHP sure doesn't say why it starts from 0 after the (-ive) start_index,but but but,if i try to array_fill from -2 index for an array that already has -1 and 0 index defined, the original array vanishes.

PHP肯定没有说明为什么它从(-ive)start_index之后的0开始,但是,但是,如果我尝试从-2索引的array_fill为已经定义了-1和0索引的数组,原始数组消失。

$a = array('-1'=>'harry','0'=>'alex');  
$a = array_fill(-2,4,'roger');

print_r($a); 

output

产量

Array ( [-2] => Roger [0] => Roger [1] => Roger [2] => Roger ) 

and $a[-1] is an undefined offset error. $a[0] is overwritten to roger.

和$ a [-1]是未定义的偏移量错误。 $ a [0]被覆盖到罗杰。

Any explanations?? And working tips for future for similar situations..!!

任何解释??以及针对类似情况的未来工作提示.. !!

3 个解决方案

#1


1  

You can try array_replace

你可以试试array_replace

$a = array('-1'=>'harry','0'=>'alex');
$b = array_fill(-2, 4, 'Roger');

$c = array_replace($b, $a);
ksort($c);
print_r($c);

Output

产量

Array
(
    [-2] => Roger 
    [-1] => harry
    [0] => alex
    [1] => Roger 
    [2] => Roger 
)

#2


1  

From array_fill manual:

从array_fill手册:

If start_index is negative, the first index of the returned array will be start_index and the following indices will start from zero (see example).

如果start_index为负数,则返回数组的第一个索引为start_index,后面的索引将从零开始(参见示例)。

<?php
$a = array_fill(5, 6, 'banana');
$b = array_fill(-2, 4, 'pear');
print_r($a);
print_r($b);
?>

Output:

输出:

Array
(
    [5]  => banana
    [6]  => banana
    [7]  => banana
    [8]  => banana
    [9]  => banana
    [10] => banana
)
Array
(
    [-2] => pear
    [0] => pear
    [1] => pear
    [2] => pear
)

So your output is correct. Next thing is that you are omitting first array. You should try with:

所以你的输出是正确的。接下来就是你省略了第一个数组。你应该尝试:

$a = array(-1 => 'harry', 0 => 'alex');
$a = array_merge(array_fill(-2, 4, 'roger'), $a);

ksort($a);

#3


0  

This is the correct method

这是正确的方法

$a = array('-1'=>'harry','0'=>'alex');  
$a = array_merge($a,array_fill(-2,4,'roger'));

print_r($a); 

if arrk keys must be preserved

如果必须保留arrk键

$a = array('-1'=>'harry','0'=>'alex');  
$b=array_fill('-2','4','roger');
    $a =  $a+ $b;

    print_r($a);

#1


1  

You can try array_replace

你可以试试array_replace

$a = array('-1'=>'harry','0'=>'alex');
$b = array_fill(-2, 4, 'Roger');

$c = array_replace($b, $a);
ksort($c);
print_r($c);

Output

产量

Array
(
    [-2] => Roger 
    [-1] => harry
    [0] => alex
    [1] => Roger 
    [2] => Roger 
)

#2


1  

From array_fill manual:

从array_fill手册:

If start_index is negative, the first index of the returned array will be start_index and the following indices will start from zero (see example).

如果start_index为负数,则返回数组的第一个索引为start_index,后面的索引将从零开始(参见示例)。

<?php
$a = array_fill(5, 6, 'banana');
$b = array_fill(-2, 4, 'pear');
print_r($a);
print_r($b);
?>

Output:

输出:

Array
(
    [5]  => banana
    [6]  => banana
    [7]  => banana
    [8]  => banana
    [9]  => banana
    [10] => banana
)
Array
(
    [-2] => pear
    [0] => pear
    [1] => pear
    [2] => pear
)

So your output is correct. Next thing is that you are omitting first array. You should try with:

所以你的输出是正确的。接下来就是你省略了第一个数组。你应该尝试:

$a = array(-1 => 'harry', 0 => 'alex');
$a = array_merge(array_fill(-2, 4, 'roger'), $a);

ksort($a);

#3


0  

This is the correct method

这是正确的方法

$a = array('-1'=>'harry','0'=>'alex');  
$a = array_merge($a,array_fill(-2,4,'roger'));

print_r($a); 

if arrk keys must be preserved

如果必须保留arrk键

$a = array('-1'=>'harry','0'=>'alex');  
$b=array_fill('-2','4','roger');
    $a =  $a+ $b;

    print_r($a);