php PHP删除数组的第一个索引并重新索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3003259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP remove the first index of an array and re-index
提问by Mithun Sreedharan
I have an array like Array
我有一个像 Array 的数组
(
[0] => A
[2] => B
[4] => C
[6] => D
)
I want to remove the first element and then re-index array to get the output
我想删除第一个元素,然后重新索引数组以获取输出
(
[0] => B
[1] => C
[2] => D
)
Which PHP function i need to use?
我需要使用哪个 PHP 函数?
Update
更新
Input array is
输入数组是
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
)
)
And the output should be like
输出应该是这样的
Array
(
[0] => Array
(
[0] => My belowed text
[1] => You crazyy
)
[1] => Array
(
[0] => My loved quote
[1] => You crazyy
)
)
回答by User123
回答by Epeli
With array_splice.
与array_splice。
http://www.php.net/manual/en/function.array-splice.php
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
)
回答by Mahbub Alam
You can cut the array as many many index as you want
您可以根据需要剪切数组尽可能多的索引
$newArray = array_splice($oldArray, $startIndex, $lengthToSlice);
回答by Vishal Solanki
we can do it with array_shift()which will remove the 1st index of array and after that use array_values()which will re-index the array values as i did not get from the @User123's answer, try below one:
我们可以这样做,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);
Output:check the output here https://eval.in/837709
输出:在此处检查输出https://eval.in/837709
Array
(
[0] => B
[1] => C
[2] => D
)
Same for your Updated Input array
与更新的输入数组相同
<?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);
Output:check the output here https://eval.in/837711
输出:在此处检查输出https://eval.in/837711
Array
(
[0] => Array
(
[0] => My belowed text
[1] => You crazyy
)
[1] => Array
(
[0] => My loved quote
[1] => You crazyy
)
)
回答by pr1nc3
$array=array(
0 => 'A',
2 => 'B',
4 => 'C',
6 => 'D'
);
unset($array[0]);
$array = array_values($array);
print_r($array);
This is also another solution to this issue using unset
这也是使用此问题的另一种解决方案 unset
Output:
输出:
Array
(
[0] => B
[1] => C
[2] => D
)

