doukong9982 2013-09-03 18:45
浏览 34
已采纳

根据数组索引将一个数组插入2个表中

I have one array, and it has 10+ indexes.

What I want to do is set variable $table based on the index's, so that it will insert

Array[0] - Array[9] to $table = table1

and it would insert

Array[10] - Array[14] to $table = table2

I don't want to use an if statement because I need them both inserted

I was hoping to keep this all in one query and use $table (if it's possible)

How could I achieve this?

  • 写回答

3条回答 默认 最新

  • dou6495 2013-09-03 19:55
    关注
    $table = array();
    
    foreach($array as $key => $value)
      if ($key <= 9)
        $table['table1'][$key] = $value;
      else
        $table['table2'][$key] = $value;
    

    This will keep it all as one array. Which I think is kinda what you were going for.

    I'm terrible at SQL query so the bellow is just pseudo for if each key is a column in the table:

    foreach($table as $key => $value){
        if($key == 'table1'){
            foreach($value as $key => $value){
                //INSERT INTO table1 ($key) VALUES ($value) 
            {
        if($key == 'table2'){
            foreach($value as $key => $value){
                //INSERT INTO table2 ($key) VALUES ($value) 
            {
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂