如何为关联数组赋值

时间:2016-03-29 14:20:57

标签: php codeigniter

我有一个名为$data的关联数组和一个名为sendToView()的函数,它接受$email$vehicleid作为参数。我需要将这些参数分配给$data数组。我怎么能这样做?

这里我已经提到过我尝试了但是它正在给“试图获取非对象的属性”错误。我该如何解决?

public function sendToView($email, $vehicleid) {        
    $data['details'] = array(
        'email' => $email,
        'id'    => $vehicleid
    );

 $this->load->view('pages/OfferView',$data);
}

offerView.php

<?php foreach ($details as $detail) {?>



                        <form name="myform4" action="<?php echo base_url(). 'OfferCtrl/sendEmailToReview/'. $detail->email .'/'. $detail->id ;?> " method="POST">

我在offerView.php.Is中这样做了,因为这个错误发生?

2 个答案:

答案 0 :(得分:1)

当您刚刚传递数据数组时,可以通过foreach循环访问它

$email = $data['details']['email']; 
$id = $data['details']['id'];

答案 1 :(得分:0)

public function sendToView($email, $vehicleid) {        
   $data['details'] = array();
   $data['details']['email'] = $email;
   $data['details']['id'] = $vehicleid;

 $this->load->view('pages/OfferView',$data); //load it into the view
}

访问它时,您可以循环并将数据推送回变量 即

$email = $details['email'];
$id = $details['id'];

然后,您可以继续在代码中使用变量$ email和$ id。 像这样:

<form name="myform4" action="<?php echo base_url(). 'OfferCtrl/sendEmailToReview/'. $email .'/'. $>id ;?> " method="POST">