我有一个类似的数组 阵列
(
[0] => A
[2] => B
[4] => C
[6] => D
)
我想删除第一个元素,然后重新索引数组以获取输出
(
[0] => B
[1] => C
[2] => D
)
我需要使用哪种PHP功能?
更新
输入数组是
Array
(
[0] => Array
(
[0] => Some Unwanted text
[1] => You crazyy
)
[2] => Array
(
[0] => My belowed text
[1] => You crazyy
)
[10] => Array
(
[0] => My loved quote
[1] => You crazyy
)
)
输出应该像
Array
(
[0] => Array
(
[0] => My belowed text
[1] => You crazyy
)
[1] => Array
(
[0] => My loved quote
[1] => You crazyy
)
)
答案 0 :(得分:34)
答案 1 :(得分:22)
使用array_splice。
http://www.php.net/manual/en/function.array-splice.php
php > print_r($input); Array ( [0] => A [2] => B [4] => C [6] => D ) php > array_splice($input, 0, 1); php > print_r($input); Array ( [0] => B [1] => C [2] => D )
答案 2 :(得分:2)
您可以根据需要将数组剪切为多个索引
$newArray = array_splice($oldArray, $startIndex, $lengthToSlice);
答案 3 :(得分:2)
我们可以使用array_shift()
来删除数组的第一个索引,之后使用array_values()
将重新索引数组值,因为我没有从@ User123的答案中获取,尝试低于一:
<?php
$array = array(
0 => "A",
2 => "B",
4 => "C",
6 => "D"
);
array_shift($array);
$array = array_values($array);
echo "<pre>";
print_r($array);
输出检查此处的输出https://eval.in/837709
Array
(
[0] => B
[1] => C
[2] => D
)
更新的输入数组
相同<?php
$array = array(
0 => array(
0 => "Some Unwanted text",
1 => "You crazyy"
),
2 => array(
0 => "My belowed text",
1 => "You crazyy"
),
10 => array(
0 => "My loved quote",
1 => "You crazyy"
)
);
array_shift($array);
$array = array_values($array);
echo "<pre>";
print_r($array);
输出检查此处的输出https://eval.in/837711
Array
(
[0] => Array
(
[0] => My belowed text
[1] => You crazyy
)
[1] => Array
(
[0] => My loved quote
[1] => You crazyy
)
)
答案 4 :(得分:0)
$array=array(
0 => 'A',
2 => 'B',
4 => 'C',
6 => 'D'
);
unset($array[0]);
$array = array_values($array);
print_r($array);
这也是使用unset
<强>输出:强>
Array
(
[0] => B
[1] => C
[2] => D
)