【问题标题】:insert all of an associative array in database in php在php中的数据库中插入所有关联数组
【发布时间】:2015-06-21 03:31:33
【问题描述】:

我想在我的表中插入所有关联数组,数组的每个索引都将在表的一行中。

这是我的代码:

public function confirmQuestion($id_array)
    {
        $quiz_id = explode(',',$id_array);

        $lesson = array();
        foreach($quiz_id as $q)
        {
            $lesson[] = DB::select("select lesson_id from quiz where '$q' = id");
        }

        $lesson_id = array();
        foreach($lesson as $key=>$value)
        {
            $lesson_id[] = $value[0]->lesson_id;
        }

        $data = array_combine($quiz_id , $lesson_id);

    }

我希望将 $data 插入我的表中。一行表中数组的每个索引。

这是我的关联数组:

array(3) { [10]=> int(15) [11]=> int(15) [12]=> int(15) }

【问题讨论】:

  • 需要把数组改成字符串

标签: php mysql arrays laravel-4


【解决方案1】:

首先,您能告诉我们更多关于上下文的信息吗? 你使用框架吗? 您能否更准确地了解数组中的数据以及表格的外观?

编辑:因为您使用的是 laravel 4 并根据页面 http://laravel.com/docs/4.2/queries

将记录插入表中

DB::table('users')->insert(
    array('email' => 'john@example.com', 'votes' => 0)
);

在一个表中插入多条记录

DB::table('users')->insert(array(
    array('email' => 'taylor@example.com', 'votes' => 0),
    array('email' => 'dayle@example.com', 'votes' => 0),
));

【讨论】:

  • 是的,不能评论这个问题,不明白为什么你需要声誉才能这样做......但编辑确实提供了答案;)
猜你喜欢
  • 2012-11-10
  • 1970-01-01
  • 2023-03-29
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-29
相关资源
最近更新 更多