如何将子数组键的第一个字符大写
how to capitalize first character of sub array keys
Array
(
[Carriers] => Array
(
[Carrier] => Array
(
[0] => Array
(
[carriersid] => a2e01423-2e8f-4458-8c13-2bfd08591d75
[phonenumber] => 1234567890
[network] => CELLCO PARTNERSHIP DBA VERIZON WIRELESS - CA
[wireless] => True
[zipcode] => 92675
[city] => Capistrano Valley
[price] => 3.0000
[createddate] => 2016-02-19 06:18:56
)
我想将[carriersid]大写为Carriersid。
我知道 array_change_key_case 函数在那里,但它只使键为大写或小写。
任何帮助深表感谢。 :)
这帮助你改变数组的键首字母大写
<?php
foreach ($arr as $key => &$val) {
$val = ucfirst($val);
}
?>
试试这个,首字母大写
<?php
$arr = ['asd'=> '123', 'sdf' => '444'];
$data = [];
foreach ($arr as $key => $val) {
$data[ucfirst($key)] = $val;
}
print_r($data);
?>
这也是可能的解决方案,如果你想要这样的 CarrieSid,那么请使用以下内容,否则使用 Rahul 发布的代码 -
$carrier_arr = $arr['Carriers']['Carrier'];
$final_arr = array();
for($i=0;$i<count($carrier_arr);$i++){
$final_arr[$i]['Carriersid'] = $carrier_arr[$i]['carriersid'];
$final_arr[$i]['Phonenumber'] = $carrier_arr[$i]['phonenumber'];
$final_arr[$i]['Network'] = $carrier_arr[$i]['network'];
$final_arr[$i]['Wireless'] = $carrier_arr[$i]['wireless'];
$final_arr[$i]['Zipcode'] = $carrier_arr[$i]['zipcode'];
$final_arr[$i]['City'] = $arr[$i]['city'];
$final_arr[$i]['Price'] = $carrier_arr[$i]['price'];
$final_arr[$i]['Createddate'] = $carrier_arr[$i]['createddate'];
}
Array
(
[Carriers] => Array
(
[Carrier] => Array
(
[0] => Array
(
[carriersid] => a2e01423-2e8f-4458-8c13-2bfd08591d75
[phonenumber] => 1234567890
[network] => CELLCO PARTNERSHIP DBA VERIZON WIRELESS - CA
[wireless] => True
[zipcode] => 92675
[city] => Capistrano Valley
[price] => 3.0000
[createddate] => 2016-02-19 06:18:56
)
我想将[carriersid]大写为Carriersid。 我知道 array_change_key_case 函数在那里,但它只使键为大写或小写。 任何帮助深表感谢。 :)
这帮助你改变数组的键首字母大写
<?php
foreach ($arr as $key => &$val) {
$val = ucfirst($val);
}
?>
试试这个,首字母大写
<?php
$arr = ['asd'=> '123', 'sdf' => '444'];
$data = [];
foreach ($arr as $key => $val) {
$data[ucfirst($key)] = $val;
}
print_r($data);
?>
这也是可能的解决方案,如果你想要这样的 CarrieSid,那么请使用以下内容,否则使用 Rahul 发布的代码 -
$carrier_arr = $arr['Carriers']['Carrier'];
$final_arr = array();
for($i=0;$i<count($carrier_arr);$i++){
$final_arr[$i]['Carriersid'] = $carrier_arr[$i]['carriersid'];
$final_arr[$i]['Phonenumber'] = $carrier_arr[$i]['phonenumber'];
$final_arr[$i]['Network'] = $carrier_arr[$i]['network'];
$final_arr[$i]['Wireless'] = $carrier_arr[$i]['wireless'];
$final_arr[$i]['Zipcode'] = $carrier_arr[$i]['zipcode'];
$final_arr[$i]['City'] = $arr[$i]['city'];
$final_arr[$i]['Price'] = $carrier_arr[$i]['price'];
$final_arr[$i]['Createddate'] = $carrier_arr[$i]['createddate'];
}