Tuesday, February 14, 2012

Remove the parent array key in PHP?


I have an array structure like this :




Array (

[donate] => Array

(
[amount_other] => 222

[pay_method] => Array
(
[5] => Array
(
[first_name] => sam
[last_name] => indi
[cc_type] => mc
[cc_number] => 5123456789012346
[cc_ccv2] => 111
[cc_exp_month] => 10
[cc_exp_year] => 20
)

)

[notes] => Test comment.
)



)



I want to remove key [5] from the array, so that the new array becomes :




Array



(




[donate] => Array

(
[amount_other] => 222
[pay_method] => Array

(
[first_name] => sam
[last_name] => indi
[cc_type] => mc
[cc_number] => 5123456789012346
[cc_ccv2] => 111
[cc_exp_month] => 10
[cc_exp_year] => 20
)

[notes] => Test comment.
)



)



I want this because the array key changes and I want to access the inner array directly so that I don't have to change the key each time in the code. If there are other ways to achieve this.. Please help. Thanks in advance.

2 comments:

  1. $array['donate']['pay_method'] = current($array['donate']['pay_method']);

    ReplyDelete
  2. $array['donate']['pay_method'] = array_shift($array['donate']['pay_method']);

    ReplyDelete