In PHP, how do you change the key of an array element?

后端 未结 23 2340
逝去的感伤
逝去的感伤 2020-11-22 03:45

I have an associative array in the form key => value where key is a numerical value, however it is not a sequential numerical value. The key is actually an I

相关标签:
23条回答
  • 2020-11-22 04:32
    $array = [
        'old1' => 1
        'old2' => 2
    ];
    
    $renameMap = [
        'old1' => 'new1',   
        'old2' => 'new2'
    ];
    
    $array = array_combine(array_map(function($el) use ($renameMap) {
        return $renameMap[$el];
    }, array_keys($array)), array_values($array));
    
    /*
    $array = [
        'new1' => 1
        'new2' => 2
    ];
    */
    
    0 讨论(0)
  • 2020-11-22 04:32

    This basic function handles swapping array keys and keeping the array in the original order...

    public function keySwap(array $resource, array $keys)
    {
        $newResource = [];
    
        foreach($resource as $k => $r){
            if(array_key_exists($k,$keys)){
                $newResource[$keys[$k]] = $r;
            }else{
                $newResource[$k] = $r;
            }
        }
    
        return $newResource;
    }
    

    You could then loop through and swap all 'a' keys with 'z' for example...

    $inputs = [
      0 => ['a'=>'1','b'=>'2'],
      1 => ['a'=>'3','b'=>'4']
    ]
    
    $keySwap = ['a'=>'z'];
    
    foreach($inputs as $k=>$i){
        $inputs[$k] = $this->keySwap($i,$keySwap);
    }
    
    0 讨论(0)
  • 2020-11-22 04:33

    this code will help to change the oldkey to new one

    $i = 0;
    $keys_array=array("0"=>"one","1"=>"two");
    
    $keys = array_keys($keys_array);
    
    for($i=0;$i<count($keys);$i++) {
        $keys_array[$keys_array[$i]]=$keys_array[$i];
        unset($keys_array[$i]);
    }
    print_r($keys_array);
    

    display like

    $keys_array=array("one"=>"one","two"=>"two");
    
    0 讨论(0)
  • 2020-11-22 04:35

    If you want to replace several keys at once (preserving order):

    /**
     * Rename keys of an array
     * @param array $array (asoc)
     * @param array $replacement_keys (indexed)
     * @return array
     */
    function rename_keys($array, $replacement_keys)  {
          return array_combine($replacement_keys, array_values($array));
    }
    

    Usage:

    $myarr = array("a" => 22, "b" => 144, "c" => 43);
    $newkeys = array("x","y","z");
    print_r(rename_keys($myarr, $newkeys));
    //must return: array("x" => 22, "y" => 144, "z" => 43);
    
    0 讨论(0)
  • 2020-11-22 04:37

    You could use a second associative array that maps human readable names to the id's. That would also provide a Many to 1 relationship. Then do something like this:

    echo 'Widgets: ' . $data[$humanreadbleMapping['Widgets']];
    
    0 讨论(0)
  • 2020-11-22 04:37

    If you want also the position of the new array key to be the same as the old one you can do this:

    function change_array_key( $array, $old_key, $new_key) {
        if(!is_array($array)){ print 'You must enter a array as a haystack!'; exit; }
        if(!array_key_exists($old_key, $array)){
            return $array;
        }
    
        $key_pos = array_search($old_key, array_keys($array));
        $arr_before = array_slice($array, 0, $key_pos);
        $arr_after = array_slice($array, $key_pos + 1);
        $arr_renamed = array($new_key => $array[$old_key]);
    
        return $arr_before + $arr_renamed + $arr_after;
    }
    
    0 讨论(0)
提交回复
热议问题