如何在PHP中向数组中的一个添加函数


How to add a function to one of array in PHP

我有一个数组$field。

注意:有人在标签中添加了codeigniter,但问题本身是关于PHP的。

通常以这种方式保存内容。

// SAVE FORM CONTENTS
foreach ($fields_to_show as $key=>$value) {
   $this->CI->preference->set_item($key,$this->CI->input->post($key));
}

这是方法set_item()

    function set_item($name, $value)
    {
        if (is_null($name)) {
           return false;
        }
        $this->preferenceCache[$name] = $value;
        if (is_array($value)) {
               $value = $this->object_keyword . serialize($value);
        }
        $this->db->where('name', $name);
        return $this->db->update(PREFERENCES, array('value'=>$value)); 
}

现在其中一个字段是密码,所以我想对它进行编码

方法encode_password()运行良好(我在另一个类中使用它),但当我尝试以下操作时,它不会对"ga_password"进行编码

// set ga_password
if ($this->CI->input->post('ga_password') != '') {
   // Load userlib language
   $this->CI->load->module_library('auth','userlib');
   $fields_to_show['ga_password'] =   
       $this->CI->userlib->encode_password($this->CI->input->post('ga_password'));
}
// SAVE FORM CONTENTS
foreach ($fields_to_show as $key=>$value) {
   $this->CI->preference->set_item($key,$this->CI->input->post($key));
}

因此您使用的方法返回密码作为编码方法的输入。

因此,如果encode方法使用pass-by-reference对输入进行编码,我想你会得到一个错误,如果它是为了返回一个编码的值,那么你需要做一些更像这样的事情:

$encoded_password = $this->CI->userlib->encode_password($this->CI->input->post('ga_password'));