Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assoc array php mysql

Want to change array.

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

result:

     $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    

Need a kind of array

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"] unique in 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"] );   

        }  

this fetching give me only one record

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

like image 329
Vladimir Lenkov Avatar asked Nov 27 '25 08:11

Vladimir Lenkov


1 Answers

Please try this.

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"];   

}  
like image 74
phreakv6 Avatar answered Nov 28 '25 22:11

phreakv6