40

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

3
  • Do you want to remove the first element, regardless of its key, or remove the element with key = "a"? Commented Sep 2, 2013 at 16:05
  • @Ganesh: You give a wrong example in your question. What you demonstrate with the example is wrong (albeit you still mean something). Commented Sep 2, 2013 at 16:10
  • @Barmar I want to remove the first key and its values of an array without affecting other key values.. the question I have given is an example. I need to deal with such kind of arrays. Commented Sep 2, 2013 at 16:42

4 Answers 4

48

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]);
Sign up to request clarification or add additional context in comments.

7 Comments

For the example given, you do not need to reset the array (but you could also just use array_shift so ... :) )
Well, obiviously the example is just an example. Who would create an array and instantly remove its first member? Once the array's being worked with, you cannot be sure where its internal pointer sits.
Yes, you can. It's pretty predictable. But however, as it turns out @Ganesh might really have just given an example that is close to totally unrelated to what he would like to learn :)
@hakre I have now edited the question...
@Ganesh No, current() is not guaranteed to deliver back the key of the first array member (although most of the time it does). If you want to be on the safe side and avoid hard to track bugs, use my approach.
|
8
$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.

2 Comments

array_shift resets the array key values...
@Ganesh: Not for the example you gave.
2

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

1 Comment

$array[current(array_keys($array))] is far longer than it needs to be. The better way is $array[key($array)].
0
<?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);
?>

1 Comment

Thanks for your answer. I have added answer for this question with which I solved

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.