assoc array php mysql

时间:2016-06-07 11:03:39

标签: php mysql arrays fetch

想要改变数组。

    while ($row=mysql_fetch_assoc($arresult['result'])){
        $arr[] = $row;                              
        }    

结果:

     $arr =
   0 => id              => 52122
         DECLARED_FAULT => blabla
         CNT            => 55
         add_time       => 2016-06-07 13:26:02

    1 => id             => 52123
         DECLARED_FAULT => blabla2
         CNT            => 93
         add_time       => 2016-06-07 13:26:05    

需要一种数组

DECLARED_FAULT => 52122 => blabla
                  52123 => blabla2

CNT            => 52122 => 55
                  52123 => 93

ADD_TIME       => 52122 => 2016-06-07 13:26:02
                  52123 => 2016-06-07 13:26:05   

$ row [" id"]在mysql中是唯一的;

while ($row=mysql_fetch_assoc($arresult['result'])){

        $arr["DECLARED_FAULT"] = array( $row["id"] => $row["DECLARED_FAULT"] ); 
        $arr["CNT"] =            array(  $row["id"] => $row["CNT"] );       
        $arr["ADD_TIME"] =       array( $row["id"] => $row["ADD_TIME"] );   

        }  

这个抓取只给我一条记录

  

array(3){[" DECLARED_FAULT"] => array(1){[54051] =>串(71)   "布拉布拉" } [" CNT"] => array(1){[54051] =>字符串(2)" 11" }   [" ADD_TIME"] => array(1){[54051] => string(19)" 2016-06-07   13:26:02" }}

2 个答案:

答案 0 :(得分:1)

请试一试。

while ($row=mysql_fetch_assoc($arresult['result'])){

    $arr["DECLARED_FAULT"][$row["id"]] = $row["DECLARED_FAULT"];
    $arr["CNT"][$row["id"]]  =            $row["CNT"];
    $arr["ADD_TIME"][$row["id"]] =       $row["ADD_TIME"];   

}  

答案 1 :(得分:0)

你必须为此做一个额外的数组,试试这个代码;

while ($row=mysql_fetch_assoc($arresult['result'])){

    $arr["DECLARED_FAULT"][] = array( $row["id"] => $row["DECLARED_FAULT"] ); 
    $arr["CNT"][] =            array(  $row["id"] => $row["CNT"] );       
    $arr["ADD_TIME"][] =       array( $row["id"] => $row["ADD_TIME"] );   

    }  

并尝试再次转储它。现在,您将在DECLARED_FAULT中拥有3个数组,而其他数组