如何將陣列從一個函式傳遞到另一個函式,并在 Codeigniter 中添加兩個陣列?
這是我的代碼:-
這是我提交資料時使用的表單的 URL。
<form method="POST" action="<?php echo base_url('Welcome/payment_page');?>">
public function payment(){
$data['userid'] = $this->input->post('userid');
$data['payment'] = $this->input->post('payment');
if(!empty($data)){
$this->payment_page($data); //passing data into another function
}
$this->load->view('frontend/include/header');
$this->load->view('frontend/payment');
}
public function payment_page($data = ''){
$data;
$other_data=array(
'cardname' => $this->input->post('cardname'),
'cardno'=> $this->input->post('cardno'),
'cardcvv'=> $this->input->post('cardcvv'),
'cardmonth'=>$this->input->post('cardmonth'),
'cardyear' =>$this->input->post('cardyear')
);
$main_data = array_merge($data, $other_data);
}
它給了我錯誤:-
Severity: Warning
Message: array_merge(): Expected parameter 1 to be an array, string given
uj5u.com熱心網友回復:
您設定的引數payment_page($data = '')是字串,而不是陣列。你必須用一個空陣列而不是一個空字串來初始化它所以它必須像
function payment_page($data = [])
uj5u.com熱心網友回復:
用空陣列初始化
public function payment_page($data = [])
如果上面的代碼不起作用,您可以嘗試另一種方法。
public function payment_page(&$data = [])
uj5u.com熱心網友回復:
您可能忘記回傳合并后的陣列。嘗試這個:
public function payment(){
$data['userid'] = $this->input->post('userid');
$data['payment'] = $this->input->post('payment');
if(!empty($data)){
$this->payment_page($data); //passing data into another function
}
$this->load->view('frontend/include/header');
$this->load->view('frontend/payment');
}
public function payment_page($data){
$other_data=array(
'cardname' => $this->input->post('cardname'),
'cardno'=> $this->input->post('cardno'),
'cardcvv'=> $this->input->post('cardcvv'),
'cardmonth'=>$this->input->post('cardmonth'),
'cardyear' =>$this->input->post('cardyear')
);
$main_data = array_merge($data, $other_data);
return $main_data;
}
uj5u.com熱心網友回復:
Kumar,您收到“嚴重性:警告”訊息,因為您使用的是僅接受陣列的array_merge函式。所以你應該在將它提供給函式之前檢查 $data 是否是一個陣列。您可以使用is_array()函式。
public function payment_page($data){
$other_data=array(
'cardname' => $this->input->post('cardname'),
'cardno'=> $this->input->post('cardno'),
'cardcvv'=> $this->input->post('cardcvv'),
'cardmonth'=>$this->input->post('cardmonth'),
'cardyear' =>$this->input->post('cardyear')
);
if(is_array($data)) {
return array_merge($data, $other_data);
}
return $other_data;
}
uj5u.com熱心網友回復:
您在沒有引數的情況下在payment_page函式上提交表單我認為您打算將其提交給付款函式,因為您從付款函式呼叫并將陣列傳遞給payment_page
如果是這種情況,您應該考慮像這樣提交表單
<form method="POST" action="<?= site_url('Welcome/payment_page');?>">
支付功能什么都不用改所以還是這個樣子
public function payment(){
$data['userid'] = $this->input->post('userid');
$data['payment'] = $this->input->post('payment');
if(!empty($data)){
$this->payment_page($data); //passing data into another function
}
$this->load->view('frontend/include/header');
$this->load->view('frontend/payment');
}
在 payment_page 函式上嘗試像這樣處理陣列
public function payment_page($data = null){
$other_data=array(
'cardname' => $this->input->post('cardname'),
'cardno'=> $this->input->post('cardno'),
'cardcvv'=> $this->input->post('cardcvv'),
'cardmonth'=>$this->input->post('cardmonth'),
'cardyear' =>$this->input->post('cardyear')
);
$main_data = array_merge($data, $other_data);
}
我希望它能解決你的問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/326055.html
標籤:php 数组 代码点火器 参数 codeigniter-3
