Search code examples
phparrayscakephp-3.0capitalizationsub-array

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
                    )

I want to capitalize [carriersid] to Carriersid. I know array_change_key_case function is there but it only make keys either UPPERCASE or LOWERCASE. Anyhelp is much appreciated. :)


Solution

  • Here is the possible solution also if you want like this CarrieSid then use following otherwise use the code posted by 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'];
    
    }