dongzhan7909 2016-07-02 15:15
浏览 35
已采纳

php循环使用相同的键的多个数组

How can I go about getting these results to insert into a database as one record for each key name? These 2 arrays will always have the same number of records and key names.

[size_chart_data] =Array (
    [Width] =Array (
        [Small] =18
        [Medium] =20
        [Large] =22
        [X-Large] =24
        [2X-Large] =26
        [3X-Large] =28
        [4X-Large] =30
        [5X-Large] =32
    )
    [Height] =Array (
        [Small] =28
        [Medium] =29
        [Large] =30
        [X-Large] =31
        [2X-Large] =32
        [3X-Large] =33
        [4X-Large] =34
        [5X-Large] =35
    )
)

I am using the following sql to insert into the database:

$chartData = db_insert('pa_size_chart_data')
    ->fields(array(
      'width',
      'height',
    ));

Thank you so much for your time. Please let me know if anything needs clarification. Aaron

  • 写回答

2条回答 默认 最新

  • drcomwc134525 2016-07-04 14:23
    关注

    This is the answer. Very special thank you to @u_mulder and @blackandorangecat for all of your helpful thoughts!!! In the question I only included width and height. But I felt it could be more helpful for others if I included a 3rd level. So the answer includes width, height, & sleeve.

      $size_chart_data = array();
      foreach ($size_chart_data['Width'] as $key => $value) {
          // floatval() is changing string results to float
          $value = floatval($value);
    
          if (!empty($size_chart_data['Height']) && !empty($size_chart_data['Sleeve'])) {
            // floatval() is changing string results to float
            $size_chart_data['Height'][$key] = floatval($size_chart_data['Height'][$key]);
            $size_chart_data['Sleeve'][$key] = floatval($size_chart_data['Sleeve'][$key]);
            $db_data = array(
                'width' => $value,                             // this is width 
                'height' => $size_chart_data['Height'][$key],  // this is height
                'sleeve' => $size_chart_data['Sleeve'][$key],  // this is the sleeve
            );
          } elseif (!empty($size_chart_data['Height'])) {
            $size_chart_data['Height'][$key] = floatval($size_chart_data['Height'][$key]);
            $db_data = array(
                'width' => $value,                             // this is width 
                'height' => $size_chart_data['Height'][$key],  // this is height
                'sleeve' => null,                              // this is the sleeve
            );  
          } elseif (!empty($size_chart_data['Width'])){
            $db_data = array(
                'width' => $value,  // this is width 
                'height' => null,   // this is height
                'sleeve' => null,   // this is the sleeve
            );
          }            
            $sizeArr = array($db_data);
            $chartData = db_insert('pa_size_chart_data')
              ->fields(array(
                'width',
                'height',
                'sleeve',
              ));
            foreach ($sizeArr as $sizeRec) {
              $chartData->values($sizeRec);
            }
            $chartData->execute();
       }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配