Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change $key of associative array in a foreach loop in php

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.

like image 660
brianwalleshauser Avatar asked Jul 31 '14 20:07

brianwalleshauser


People also ask

How do you change the key of an associative array?

Just make a note of the old value, use unset to remove it from the array then add it with the new key and the old value pair. Save this answer.

What is the syntax of the foreach loop in case of associative array in PHP?

It loops over the array, and each value for the current array element is assigned to $value, and the array pointer is advanced by one to go the next element in the array. Syntax: <? php foreach (array as $value){ //code to be executed; } ?>

How do you foreach an associative array?

Given two arrays arr1 and arr2 of size n. The task is to iterate both arrays in the foreach loop. Both arrays can combine into a single array using a foreach loop.

How do you unset an associative array in PHP?

Method 1: Using unset() function: The unset() function is used to unset a key and its value in an associative array. print_r( $arr ); ?> Method 2: Using array_diff_key() function: This function is used to get the difference between one or more arrays.


2 Answers

unset it first in case it is already in the proper format, otherwise you will remove what you just defined:

foreach($array as $key => $value)
    {
        unset($array[$key]);
        $array[ucfirst($key)] = $value;
    }
like image 124
cmorrissey Avatar answered Sep 17 '22 18:09

cmorrissey


You can't modify the keys in a foreach, so you would need to unset the old one and create a new one. Here is another way:

$array = array_combine(array_map('ucfirst', array_keys($array)), $array);
  1. Get the keys using array_keys
  2. Apply ucfirst to the keys using array_map
  3. Combine the new keys with the values using array_combine
like image 22
AbraCadaver Avatar answered Sep 18 '22 18:09

AbraCadaver