如何在不更改其键值的情况下删除数组的第一个元素?
I have an array in php
我在php中有一个数组
<?php
$array=array("a"=>"123","b"=>"234","c"=>"345");
array_shift($array);
//array("0"=>"234","1"=>"345");
?>
If I use this function, then key value gets changed. I want my key value to remain the same. How can I remove first element without affecting array key values. My answer should be like
如果我使用此功能,则键值会发生变化。我希望我的键值保持不变。如何在不影响数组键值的情况下删除第一个元素。我的回答应该是这样的
array("b"=>"234","c"=>"345");
Note:Please do not use foreach(); I want to do this by existing array functions in php
注意:请不要使用foreach();我想通过php中现有的数组函数来做到这一点
array_splice function is working for above array. But consider the below array
array_splice函数适用于上面的数组。但请考虑以下数组
<?php
$array = Array
(
'39' => Array
(
'id' => '39',
'field_id' => '620'
),
'40' => Array
(
'id' => '40',
'field_id' => '620',
'default_value' => 'rrr',
));
array_splice($array, 0, 1);
print_r($array);
?>
It is showing answer as follows:
它的答案如下:
Array ( [0] => Array ( [id] => 40 [field_id] => 620 [default_value] => rrr ) )
May I know the reason?? Will array_splice() work only for single dimensional array?? Now key value is reset...
我可以知道原因吗? array_splice()只适用于单维数组吗?现在键值被重置......
4 个解决方案
#1
29
In case you do not know what the first item's key
is:
如果你不知道第一项的关键是什么:
// Make sure to reset the array's current index
reset($array);
$key = key($array);
unset($array[$key]);
更多相关文章
- PHP中类和对象的相关函数
- php函数 什么时候使用fflush函数
- 以完整二叉树,数组格式获取所有节点
- php 写一个算法,查寻数组里第一个大于某数的值。
- 插入PHP数组作为MYSQL列
- 如何调试UPDATE函数在PHP中无法正常工作的原因?
- PHP结束后执行函数
- php把从数据库读取出来的数据存放到数组里
- 在key/value的基础上将数组分组到另一个数组中