php中的数组级别插入

时间:2014-03-15 07:07:26

标签: php mysql sql arrays

我在php中有任务,(即)有两个数组,第一级数组键包含其插入ID。所以第二级数组值想要插入表中。

问题是我应该使用一个foreach只是因为代码优化,答案我想在表中插入第二级数组,第一级数组值(id)是第二级数组的父类。

所以我想插入对应的id和第二级数组值。数组如下:

第一级数组的值为id,键值为类别类型

     Array(
               [catws] => 2127
               [bar] => 2128
               [try] => 2129
               [tewt] => 2130
               [some] => 2131
                [cat] => 2132
             )

第二级数组具有键作为第一级键

    Array (
          [subcat] => Array
                 (
    > >                 [catws] => Array
    > >                     (
    > >                         [0] => PACKING 
    > >                     )
    > >     
    > >                 [bar] => Array
    > >                     (
    > >                         [0] => horse
    > >                         [1] => horse COIN
    > >                         [2] => horse  ARTICLE
    > >                         [3] => horse GIFT ITEM 
    > >                         [4] => horse SILVER
    > >                         [5] => horse PENDENT
    > >                         [6] => horse IDOLS
    > >                         [7] => horse ITEM PIECE PRICE
    > >                         [8] => horse PHOTO
    > >                         [9] => horse RING
    > >                         [10] => horse PRECIOUS RING
    > >                     )
    > >     
    > >                 [try] => Array
    > >                     (
    > >                        [0] => horse
    > >                         [1] => horse COINed
    > >                         [2] => horse  ARTICLEed
    > >                         [3] => horse GIFT ITEMed
    > >                         [4] => horse SILVERed
    > >                         [5] => horse PENDENTed
    > >                         [6] => horse IDOLSed
    > >                         [7] => horse ITEMed
    > >                         [9] => horse RINGed
    > >                         [10] => PRECIOUS horse
    > >                     )
    > >     
    > >                 [tewt] => Array
    > >                     (
    > >                      [0] => tewt
    > >                         [1] => tewt COINsese
    > >                         [2] => tewt  ARTICLEed
    > >                         [3] => tewt GIFT ITEMed
    > >                         [4] => tewt SILVERed
    > >                         [5] => tewt PENDENTed
    > >                         [6] => tewt IDOLSed
    > >                         [7] => tewt ITEMed
    > >                         [9] => tewt RINGed
    > >                         [10] => tewt horse
    > >                     )
    > >     
    > >                 [some] => Array
    > >                     (
    > >                         [1] => some COINsese
    > >                         [2] => some  ARTICLEed
    > >                         [3] => some tewt GIFT ITEMed
    > >                         [4] => some  SILVERed
    > >                         [5] => some PENDENTed
    > >                         [6] => some IDOLSed
    > >                         [7] => some ITEMed
    > >                         [9] => some RINGed
    > >                         [10] => some horse
    > >                       
    > >                     )
    > >     
    > >                 [cat] => Array
    > >                     (
    > >                         [1] => cat COINsese
    > >                         [2] => cat  ARTICLEed
    > >                         [3] => cat tewt GIFT ITEMed
    > >                         [4] => cat  SILVERed
    > >                         [5] => cat PENDENTed
    > >                         [6] => cat IDOLSed
    > >                         [7] => cat ITEMed
    > >                         [9] => cat RINGed
    > >                         [10] => cat horse
    > >                     )
    > >     
    > >             )
    > >     
    > >     )

1 个答案:

答案 0 :(得分:0)

我们假设

  • 您的第一个数组是$categories = array('catws' => 2127, ...)
  • 您的第二个数组是$items = array('catws' => array(0 => 'PACKING'), 'bar' => ...)
    (在你的例子中,这是数组的第二个维度)
  • 你的桌面布局是s.th.像category (int) | id (int) | name (string/varchar)

然后你将遍历你的项目以生成查询字符串的一部分(一行)或生成另一个数组以将其传递给MySQL包装器,该包装器以某种方式构建插入查询本身:

foreach($items as $catname => $cat) {
    if(!array_key_exists($catname, $categories)) {
        continue; // skip unknown categories
    }
    $catid = $categories[$catname];

    foreach($cat as $itemid => $item) {
        // generate row triple
        $rows[] = sprintf('(%d, %d, %s)', $catid, $itemid, $item);
        // OR generate array for a wrapper
        $rows[] = array(
            'category' => $catid,
            'id' => $itemid,
            'name' => $item
        );
    }
}

请注意,您必须使用嵌套循环,因为您有一个嵌套数组。

<小时/> 生成行三元组时,可以创建如下查询:

$query = sprintf(
    'INSERT INTO `tablename` (category, id, name) VALUES %s',
    implode(',', $rows)
);

另请注意在此代码示例中,没有进行值转义,因为这取决于您的MySQL实现。
一个安全的解决方案是使用一个包含数组的包装器(第二个$rows[]示例)并执行内部清理,或者 - 甚至更好 - 使用预准备语句。