在php中的foreach循环中更改关联数组的$ key

时间:2022-02-02 21:19:14

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.

几乎在我的智慧结束这个。我正在使用Magento 1.9.0.1 CE,但这与此问题无关。如果你必须知道,我必须这样做的原因是因为我有一堆对象,我将作为一个数组返回组装到SOAP客户端。我正在使用的API要求键以大写字母开头...但是,我不希望将对象的变量名的第一个字母大写。傻,我知道,但我们都回答某人,而且有人想要这样。

5 个解决方案

#1


18  

foreach($array as $key => $value)
    {
        $array[ucfirst($key)] = $value;
        unset($array[$key]);
    }

#2


8  

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

您无法修改foreach中的键,因此您需要创建一个新键并取消旧键。这是另一种方式:

$array = array_combine(array_map('ucfirst', array_keys($array)), $array);
  1. Get the keys using array_keys
  2. 使用array_keys获取密钥
  3. Apply ucfirst to the keys using array_map
  4. 使用array_map将ucfirst应用于键
  5. Combine the new keys with the values using array_combine
  6. 使用array_combine将新键与值组合在一起

#3


1  

This might work:

这可能有效:

foreach($array as $key => $value) {
     $newkey = ucfirst($key);
     $array[$newkey] = $value;
     unset($array[$key]);
}

but it is very risky to modify an array like this while you're looping on it. You might be better off to store the unsettable keys in another array, then have a separate loop to remove them from the original array.

但是当你循环它时修改这样的数组是非常危险的。最好将不可设置的密钥存储在另一个数组中,然后使用单独的循环将它们从原始数组中删除。

And of course, this doesn't check for possible collisions in the aray, e.g. firstname -> FirstName, where FirstName already existed elsewhere in the array.

当然,这并不能检查aray中可能的碰撞,例如firstname - > FirstName,其中FirstName已经存在于数组的其他位置。

But in the end, it boils down to the fact that you can't "rename" a key. You can create a new one and delete the original, but you can't in-place modify the key, because the key IS the key to lookup an entry in the aray. changing the key's value necessarily changes what that key is pointing at.

但最终,它归结为你不能“重命名”一个键的事实。您可以创建一个新的并删除原始文件,但是您无法就地修改密钥,因为密钥是在aray中查找条目的关键。更改密钥的值必然会更改密钥指向的内容。

#4


1  

Top of my head...

我的头脑......

foreach($array as $key => $value){
    $newKey = ucfirst($key);
    $array[$newKey] = $value;
    unset($array[$key]);
}

Slightly change your way of thinking. Instead of modifying an existing element, create a new one, and remove the old one.

略微改变你的思维方式。而不是修改现有元素,创建一个新元素,并删除旧元素。

#5


0  

The answers here are dangerous, in the event that the key isn't changed, the element is actually deleted from the array. Also, you could unknowingly overwrite an element that was already there.

这里的答案是危险的,如果密钥没有改变,元素实际上是从数组中删除的。此外,您可能会在不知不觉中覆盖已存在的元素。

You'll want to do some checks first:

你首先要做一些检查:

foreach($array as $key => $value)
{
    $newKey = ucfirst($key);

    // does this key already exist in the array?
    if(isset($array[$newKey])){
        // yes, skip this to avoid overwritting an array element!
        continue;
    }

    // Is the new key different from the old key?
    if($key === $newKey){
        // no, skip this since the key was already what we wanted.
        continue;
    }

    $array[$newKey] = $value;
    unset($array[$key]);
}

Of course, you'll probably want to combine these "if" statements with an "or" if you don't need to handle these situations differently.

当然,如果您不需要以不同的方式处理这些情况,您可能希望将这些“if”语句与“或”结合起来。

#1


18  

foreach($array as $key => $value)
    {
        $array[ucfirst($key)] = $value;
        unset($array[$key]);
    }

#2


8  

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

您无法修改foreach中的键,因此您需要创建一个新键并取消旧键。这是另一种方式:

$array = array_combine(array_map('ucfirst', array_keys($array)), $array);
  1. Get the keys using array_keys
  2. 使用array_keys获取密钥
  3. Apply ucfirst to the keys using array_map
  4. 使用array_map将ucfirst应用于键
  5. Combine the new keys with the values using array_combine
  6. 使用array_combine将新键与值组合在一起

#3


1  

This might work:

这可能有效:

foreach($array as $key => $value) {
     $newkey = ucfirst($key);
     $array[$newkey] = $value;
     unset($array[$key]);
}

but it is very risky to modify an array like this while you're looping on it. You might be better off to store the unsettable keys in another array, then have a separate loop to remove them from the original array.

但是当你循环它时修改这样的数组是非常危险的。最好将不可设置的密钥存储在另一个数组中,然后使用单独的循环将它们从原始数组中删除。

And of course, this doesn't check for possible collisions in the aray, e.g. firstname -> FirstName, where FirstName already existed elsewhere in the array.

当然,这并不能检查aray中可能的碰撞,例如firstname - > FirstName,其中FirstName已经存在于数组的其他位置。

But in the end, it boils down to the fact that you can't "rename" a key. You can create a new one and delete the original, but you can't in-place modify the key, because the key IS the key to lookup an entry in the aray. changing the key's value necessarily changes what that key is pointing at.

但最终,它归结为你不能“重命名”一个键的事实。您可以创建一个新的并删除原始文件,但是您无法就地修改密钥,因为密钥是在aray中查找条目的关键。更改密钥的值必然会更改密钥指向的内容。

#4


1  

Top of my head...

我的头脑......

foreach($array as $key => $value){
    $newKey = ucfirst($key);
    $array[$newKey] = $value;
    unset($array[$key]);
}

Slightly change your way of thinking. Instead of modifying an existing element, create a new one, and remove the old one.

略微改变你的思维方式。而不是修改现有元素,创建一个新元素,并删除旧元素。

#5


0  

The answers here are dangerous, in the event that the key isn't changed, the element is actually deleted from the array. Also, you could unknowingly overwrite an element that was already there.

这里的答案是危险的,如果密钥没有改变,元素实际上是从数组中删除的。此外,您可能会在不知不觉中覆盖已存在的元素。

You'll want to do some checks first:

你首先要做一些检查:

foreach($array as $key => $value)
{
    $newKey = ucfirst($key);

    // does this key already exist in the array?
    if(isset($array[$newKey])){
        // yes, skip this to avoid overwritting an array element!
        continue;
    }

    // Is the new key different from the old key?
    if($key === $newKey){
        // no, skip this since the key was already what we wanted.
        continue;
    }

    $array[$newKey] = $value;
    unset($array[$key]);
}

Of course, you'll probably want to combine these "if" statements with an "or" if you don't need to handle these situations differently.

当然,如果您不需要以不同的方式处理这些情况,您可能希望将这些“if”语句与“或”结合起来。