【问题标题】:php loop through multiple arrays with same keyphp循环遍历具有相同键的多个数组
【发布时间】:2016-07-02 15:15:12
【问题描述】:

如何将这些结果作为每个键名的一条记录插入数据库? 这 2 个数组将始终具有相同数量的记录和键名。

[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
    )
)

我正在使用以下sql插入数据库:

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

非常感谢您抽出宝贵时间。如果有任何需要澄清的地方,请告诉我。 亚伦

【问题讨论】:

  • 什么是size_id
  • size_id 对于这个问题并不重要,我已经编辑了这个问题。很抱歉造成混乱。

标签: php database for-loop foreach drupal-7


【解决方案1】:

我想你应该这样做:

$size_chart_data = array();
foreach ($size_chart_data['Width'] as $key => $value) {
    $db_data = array();
    if (!empty($size_chart_data['Height'][$key])) {
        $db_data = array(
            'size_id' => $key,                             // this is size_id
            'width' => $value,                             // this is width 
            'height' => $size_chart_data['Height'][$key],  // this is height
        );
        // check what you've got
        print_r($db_data);
        // add $db_data to database, I suppose it's like
        $chartData = db_insert('pa_size_chart_data')
            ->fields($db_data);
    }
}

【讨论】:

  • 谢谢 U_Mulder!它将数据放入可用的顺序中。由于某种原因,数据没有被插入到数据库中。我会做更多的调查,希望能够弄清楚。
  • 可能因为是多维数组而无法使用? @aaronb
  • $singleArray = call_user_func_array('array_merge', $multiArray); @aaronb 这将使阵列变平。你可以在插入数据库之前试试这个
  • 感谢您的想法!我一直在尝试使用您建议的功能。你能告诉我你将如何在这个特定的例子中使用它吗?谢谢! @blackandorangecat
  • 如果你想看看函数做了什么,把它放在print_r($db_data); $db_data_flat = call_user_func_array('array_merge', $db_data); print_r($db_data_flat)` 然后将“字段”内的值更改为 $db_data_flat。 @aaronb 我不确定这是否会解决它,但这是一个想法
【解决方案2】:

这就是答案。非常特别感谢@u_mulder 和@blackandorangecat 的所有有益想法!!! 在问题中,我只包括宽度和高度。但我觉得如果我包括第 3 级,它可能对其他人更有帮助。所以答案包括宽度、高度和袖子。

  $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();
   }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 2012-02-28
    • 1970-01-01
    • 2012-04-21
    相关资源
    最近更新 更多