如何解决将数组传递给函数参数
| 可以在函数参数中传递数组吗?例如,function something()
{
if ($this->db->insert($this->table_name,$data)) {
$data = array(
\'user_id\' => $this->db->insert_id();
\'first_name\' => $this->form_validation->set_value(\'first_name\'),\'last_name\' => $this->form_validation->set_value(\'last_name\'),);
if ($activated)
$this->create_profile($data);
return array( //not sure why i\'m returning this
\'user_id\' => $user_id,\'first_name\' => $first_name,\'last_name\' => $last_name,} );
return NULL;
}
然后将其传递给
private function create_profile($data)
{
return $this->db->insert( $this->profile_table_name,$data )
}
该脚本来自我修改过的codeigniter插件,因此我尝试不要过多地屠宰它。
解决方法
将数组传递给函数绝对好。 PHP自己的内置
array_*()
函数中的许多函数接收数组参数,然后返回数组。
至于从函数返回数组-这是从单个函数返回多个值的绝佳方法。
,可以在函数参数中传递数组吗? =>是的!
,如果您需要返回多个值,则使用数组是一种方法。
您还可以返回一个对象,只需实例化stdClass
并设置属性即可。
,可以传递array
作为参数,也可以返回结果array
。在PHP
中,我们一直都这样做。
也可以用其他语言传递和返回数组...就像在Python
中那样,他们经常这样做,例如process_parameters(*parameters)
。
传递对象甚至还可以!或返回一个对象...像这样:
$dbConnection = get_db_connection(\'mysql\'); // $dbConnection is an instance of MySQLDbConnection now
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。