38

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

Ganesh Babu
  • 3,590
  • 11
  • 34
  • 67
  • Do you want to remove the first element, regardless of its key, or remove the element with key = "a"? – Barmar Sep 02 '13 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). – hakre Sep 02 '13 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. – Ganesh Babu Sep 02 '13 at 16:42

4 Answers4

47

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]);
aefxx
  • 24,835
  • 6
  • 45
  • 55
  • 1
    For the example given, you do not need to reset the array (but you could also just use `array_shift` so ... :) ) – hakre Sep 02 '13 at 16:19
  • 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. – aefxx Sep 02 '13 at 16:22
  • 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 Sep 02 '13 at 16:23
  • @hakre I have now edited the question... – Ganesh Babu Sep 02 '13 at 16:40
  • @Ganesh Seems that this is your best bet, then. – aefxx Sep 02 '13 at 16:48
  • @aefxx I have found the solution and posted it as an answer. The concept is to find the current key element using current() and unset the key value.. – Ganesh Babu Sep 03 '13 at 05:54
  • 2
    @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. – aefxx Sep 03 '13 at 08:46
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.

sybear
  • 7,837
  • 1
  • 22
  • 38
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..

Ganesh Babu
  • 3,590
  • 11
  • 34
  • 67
  • 2
    `$array[current(array_keys($array))]` is far longer than it needs to be. The better way is `$array[key($array)]`. – spex Dec 04 '13 at 20:12
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);
?>
Yatin Trivedi
  • 661
  • 6
  • 9