How to rename array keys in PHP?

前端 未结 10 1175
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 03:39

When I var_dump on a variable called $tags (a multidimensional array) I get this:

Array
(
    [0] => Array
        (
            [name] => tabbing
            [url         


        
相关标签:
10条回答
  • 2020-11-28 04:17

    Very simple approach to replace keys in a multidimensional array, and maybe even a bit dangerous, but should work fine if you have some kind of control over the source array:

    $array = [ 'oldkey' => [ 'oldkey' => 'wow'] ];
    $new_array = json_decode(str_replace('"oldkey":', '"newkey":', json_encode($array)));
    print_r($new_array); // [ 'newkey' => [ 'newkey' => 'wow'] ]
    
    0 讨论(0)
  • 2020-11-28 04:18
    class DataHelper{
    
        private static function __renameArrayKeysRecursive($map = [], &$array = [], $level = 0, &$storage = []) {
            foreach ($map as $old => $new) {
                $old = preg_replace('/([\.]{1}+)$/', '', trim($old));
                if ($new) {
                    if (!is_array($new)) {
                        $array[$new] = $array[$old];
                        $storage[$level][$old] = $new;
                        unset($array[$old]);
                    } else {
                        if (isset($array[$old])) {
                            static::__renameArrayKeysRecursive($new, $array[$old], $level + 1, $storage);
                        } else if (isset($array[$storage[$level][$old]])) {
                            static::__renameArrayKeysRecursive($new, $array[$storage[$level][$old]], $level + 1, $storage);
                        }
                    }
                }
            }
        }
    
        /**
         * Renames array keys. (add "." at the end of key in mapping array if you want rename multidimentional array key).
         * @param type $map
         * @param type $array
        */
        public static function renameArrayKeys($map = [], &$array = [])
        {
            $storage = [];
            static::__renameArrayKeysRecursive($map, $array, 0, $storage);
            unset($storage);
        }
    }
    

    Use:

    DataHelper::renameArrayKeys([
        'a' => 'b',
        'abc.' => [
           'abcd' => 'dcba'
        ]
    ], $yourArray);
    
    0 讨论(0)
  • 2020-11-28 04:20

    Loop through, set new key, unset old key.

    foreach($tags as &$val){
        $val['value'] = $val['url'];
        unset($val['url']);
    }
    
    0 讨论(0)
  • 2020-11-28 04:21

    You could use array_map() to do it.

    $tags = array_map(function($tag) {
        return array(
            'name' => $tag['name'],
            'value' => $tag['url']
        );
    }, $tags);
    
    0 讨论(0)
  • 2020-11-28 04:23

    This is how I rename keys, especially with data that has been uploaded in a spreadsheet:

    function changeKeys($array, $new_keys) {
        $newArray = [];
    
        foreach($array as $row) {
            $oldKeys = array_keys($row);
            $indexedRow = [];
    
            foreach($new_keys as $index => $newKey)
                $indexedRow[$newKey] = isset($oldKeys[$index]) ? $row[$oldKeys[$index]] : '';
    
            $newArray[] = $indexedRow;
        }
    
        return $newArray;
    }
    
    0 讨论(0)
  • 2020-11-28 04:25

    Talking about functional PHP, I have this more generic answer:

        array_map(function($arr){
            $ret = $arr;
            $ret['value'] = $ret['url'];
            unset($ret['url']);
            return $ret;
        }, $tag);
    }
    
    0 讨论(0)
提交回复
热议问题