用于进行2 sql更新查询的多维数组

时间:2014-04-24 08:41:05

标签: php mysql arrays multidimensional-array

编辑:我调整了数组,以便更容易理解。

我现在挣扎了几天,这让我发疯了......

我基本上想要的是2个sql更新查询,看起来像这样。请注意where语句来自数组。

代码:

"UPDATE gewasregistratie SET lengtegroei= 1, vruchten_geaborteerd= 1, etc.. WHERE kenmerk = 'standaard' AND user_id = ".$user""

"UPDATE gewasregistratie SET lengtegroei= 2, vruchten_geaborteerd= 2, etc.. WHERE kenmerk = 'natugro' AND user_id = ".$user""

我得到了这个多级数组(它是一个$_POST["type"]数组)。

Array
(
    [lengtegroei] => Array
        (
            [standaard] => 1
            [natugro] => 2
        )

    [vruchten_geaborteerd] => Array
        (
            [standaard] => 1
            [natugro] => 2
        )

    [plantbelasting_geteld] => Array
        (
            [standaard] => 1
            [natugro] => 2
        )

    [uitgroeiduur] => Array
        (
            [standaard] => 1
            [natugro] => 2
        )

    [percentage_85ers_en_95ers] => Array
        (
            [standaard] => 1
            [natugro] => 2
        )

    [oogstfrequentie_per_week] => Array
        (
            [standaard] => 1
            [natugro] => 2
        )

    [gezette_vruchten] => Array
        (
            [standaard] => 1
            [natugro] => 2
        )

    [gemiddeld_vruchtgewicht] => Array
        (
            [standaard] => 1
            [natugro] => 2
        )

    [vruchten_geoogst] => Array
        (
            [standaard] => 1
            [natugro] => 2
        )

    [drain] => Array
        (
            [standaard] => 1
            [natugro] => 2
        )

)

1 个答案:

答案 0 :(得分:1)

你是说这样的意思吗?

<?php
   $aAll = array();

   $aTogather = array(
       'standaard'  => array(),
       'natugro'        => array()
   );

   foreach($aPost as $sKey => $mRequest) {
       if(is_array($mRequest)) {
           foreach($mRequest as $sInnKey => $mValue)) {
               $aTogather[$sInnKey][] = $sKey . ' = ' . '"'.$mValue.'"';
           }
       }
   }

   foreach($aTogather as $sKey => $mValue) {
      echo 'UPDATE gewasregistratie SET '.implode(', ', $aTogather[$sKey]).' WHERE kenmerk = "'.$sKey.'" AND user_id = ...';
   }
?>