php数组foreach循环添加键值对,在PHP的foreach循环中更改关联数组的$ key

I have an array like this:

array(

'firstName' => 'Joe',

'lastName' => 'Smith'

)

I need to loop over every element in my array and in the end, the array should look like this:

array(

'FirstName' => 'Joe',

'LastName' => 'Smith'

)

Failed idea was:

foreach($array as $key => $value)

{

$key = ucfirst($key);

}

This obviously will not work, because the array is not passed by reference. However, all these attempts also fail:

foreach(&$array as $key => $value)

{

$key = ucfirst($key);

}

foreach($array as &$key => $value)

{

$key = ucfirst($key);

}

Pretty much at my wits end with this one. I'm using Magento 1.9.0.1 CE, but that's pretty irrelevant for this problem. If you must know, the reason I have to do this is because I have a bunch of object that's I'm returning as an array to be assembled into a SOAP client. The API I'm using requires the keys to begin with a capital letter...however, I don't wish to capitalize the first letter of my object's variable names. Silly, I know, but we all answer to someone, and that someone wants it that way.

解决方案foreach($array as $key => $value)

{

$array[ucfirst($key)] = $value;

unset($array[$key]);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值