简体   繁体   English

在 Codeigniter 中的函数之间传递数组

[英]Pass array between functions in Codeigniter

How can I pass an array from one function to the other one, and add both arrays in Codeigniter?如何将数组从一个 function 传递到另一个,并将两个 arrays 添加到 Codeigniter 中?

Here is my code:-这是我的代码:-

This is the URL of form i use when submit the data.这是我提交数据时使用的表格 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);
        
        
    }

Its giving me error:-它给我错误:-

Severity: Warning

Message: array_merge(): Expected parameter 1 to be an array, string given

The parameter you set for payment_page($data = '') is a string, not an array.您为payment_page($data = '')设置的参数是一个字符串,而不是一个数组。 You have to initialize it with an empty array instead of an empty string So it has to be like你必须用一个空数组而不是一个空字符串来初始化它所以它必须像

function payment_page($data = [])

Init with an empty array用一个空数组初始化

public function payment_page($data = [])

In case the above code not working, you can try another way.如果上面的代码不起作用,您可以尝试其他方法。

public function payment_page(&$data = [])

You probably forgot to return the merged array.您可能忘记返回合并后的数组。 Try this:尝试这个:

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;
}

Kumar, you are getting a "Severity: Warning" message because you are using the array_merge function which accepts only an array. Kumar,您收到一条“严重性:警告”消息,因为您使用的array_merge function 只接受一个数组。 SO you should check if $data is an array before supplying it to the function. You can use is_array() the function.所以你应该在将它提供给 function 之前检查 $data 是否是一个数组。你可以使用is_array() function。

 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;
}

you submit your form on payment_page function without parameters I think you meant to submit it to the payment function because you call and pass the array to payment_page from the payment function您在payment_page function 上提交您的表格时没有参数我认为您打算将其提交给付款 function 因为您调用并将数组从付款 function 传递给payment_page

if that is the case you should consider submitting your form like this如果是这种情况,您应该考虑像这样提交您的表格

<form method="POST" action="<?= site_url('Welcome/payment_page');?>">

you need to change nothing on the payment function so it's still like this您无需更改付款 function 上的任何内容,所以它仍然是这样

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');
    }

on payment_page function try to handle the array like this在 payment_page function 尝试像这样处理数组

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);
    
    
}

I hope it solve your problem我希望它能解决你的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM