gpt4 book ai didi

php - 将数组存储在数据库中

转载 作者:行者123 更新时间:2023-11-29 07:55:50 24 4
gpt4 key购买 nike

我有三个数组示例:$array1、$array2、$array3。

我想将 array2 和 array3 插入到同一个表的两个不同列中。我可以这样做吗?

下面是我正在尝试的代码,但它对我在 codeigniter 中执行的操作不起作用:

  • Controller :

     $athletes_id = $this->input->post('athletes'); // array 1
    $fields_id = $this->input->post('fields_id'); // array 2
    $athlete_score = $this->input->post('athlete_score'); // array 3


    $id = array();
    foreach($athlete_score as $row){
    $additional_data = array(
    'test_reports_id' => $test_report_id,
    'score' => $row,
    );
    $id[] = $this->test_model->save_test_reports_details($additional_data);
    }
    for($j=0;$j<count($fields_id);$j++){
    $data2 = array('fields_id' => $fields_id[$j]);
    $this->test_model->update_test_reports_details($id,$data2);
    }
  • 型号:

       public function update_test_reports_details($id,$data2){
    $this->db->where_in('id',$id);
    $this->db->update('test_reports_details',$data2);
    }

最佳答案

只需序列化数组即可。

$data2 = serialize($array2); // or whatever array you want to store

然后通过反序列化来检索

$array2 = unserialize($data2);//or the row index  

关于php - 将数组存储在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25198643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com