'How to remove the first element of array without changing its key value? [duplicate]

There's 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");

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

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

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...



Solution 1:[1]

The solution for this question is as follows:

<?php

unset($array[current(array_keys($array))]);

?>

It removes the first element without affecting the key values..

Solution 2:[2]

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

Solution 3:[3]

$array=array("a"=>"123","b"=>"234","c"=>"345");
unset($array["a"]) ;
var_dump($array) ;

Also, what version of PHP do you use?

array_shift works fine for me with string-indexed arrays and I get the expected result.

Solution 4:[4]

<?php function array_kshift(&$array)
{
list($k) = array_keys($array);
$r  = array($k=>$array[$k]);
unset($array[$k]);
return $r;
}

// test it on a simple associative array
$array=array("a"=>"123","b"=>"234","c"=>"345");

array_kshift($array);
print_r($array);
?>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ganesh Babu
Solution 2
Solution 3
Solution 4 Yatin Trivedi