如何在不更改其键值的情况下删除数组的第一个元素? [英] How to remove the first element of array without changing its key value?

查看:109
本文介绍了如何在不更改其键值的情况下删除数组的第一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在php中有一个数组

I have an array in 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");

注意:请不要使用foreach();我想通过php中现有的数组函数来做到这一点

Note:Please do not use foreach(); I want to do this by existing array functions in php

array_splice函数适用于上述数组.但是考虑下面的数组

array_splice function is working for above array. But consider the below array

<?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);
?>

它显示的答案如下:

Array ( [0] => Array ( [id] => 40 [field_id] => 620 [default_value] => rrr ) )

我可以知道原因吗? array_splice()仅适用于一维数组吗?现在,键值已重置...

May I know the reason?? Will array_splice() work only for single dimensional array?? Now key value is reset...

推荐答案

如果您不知道第一项的key是什么:

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]);

这篇关于如何在不更改其键值的情况下删除数组的第一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆