dpd46554 2018-11-14 19:39
浏览 142
已采纳

使用MySQL表中的SQL Update列的数组到列行

I have array named olmali

$olmali = $_POST['result'];

And

print_r($olmali);
Result is below:

Array ( 
    [0] => 1
    [1] => 1
    [2] => 20
    [3] => 2 
    [4] => 3
    [5] => 5
    [6] => 6 
    [7] => 7 
    [8] => 9 
    [9] => 8 
    [10] => 10
    [11] => 11
    [12] => 13
    [13] => 12 
    [14] => 12
    [15] => 14 
    [16] => 15
    [17] => 16
    [18] => 17
    [19] => 17
    [20] => 19
    [21] => 20
)

I want to use SQL UPDATE command and I expect:

id        test
1          1
2          1
3          20
4          2
5          3
6         ....and goes on

How can I resolve this problem? Is there any way and how can I do it. PHP array to column row in MySQL table with UPDATE SQL command like that

</div>
  • 写回答

2条回答 默认 最新

  • dongziduo9762 2018-11-14 19:55
    关注

    I would suggest to start from the end with a for loop and reassign every key with a value +1. Then, you just have to remove the first index of the array with unset(). See the code below.

    $olmali = [
      0 => 1,
      1 => 1,
      2 => 20,
      3 => 2,
      4 => 3,
      5 => 5,
      6 => 6,
      7 => 7,
      8 => 9,
      9 => 8,
      10 => 10,
      11 => 11,
      12 => 13,
      13 => 12,
      14 => 12,
      15 => 14,
      16 => 15,
      17 => 16,
      18 => 17,
      19 => 17,
      20 => 19,
      21 => 20
    ];
    
    for($i = count($olmali); $i > 0; $i--) {
      $olmali[$i] = $olmali[$i - 1];
    }
    
    unset($olmali[0]);
    
    print_r($olmali);
    

    Output:

    Array ( [1] => 1 [2] => 1 [3] => 20 [4] => 2 [5] => 3 [6] => 5 [7] => 6 [8] => 7 [9] => 9 [10] => 8 [11] => 10 [12] => 11 [13] => 13 [14] => 12 [15] => 12 [16] => 14 [17] => 15 [18] => 16 [19] => 17 [20] => 17 [21] => 19 [22] => 20 )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器