3

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" } }

1
  • instead of this $arresult['result'] write $query variable. Commented Jun 7, 2016 at 11:07

2 Answers 2

1

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

}  
Sign up to request clarification or add additional context in comments.

1 Comment

This one is also an option, according to what he wants not the correct one. He wants to have an array. On the other hand, technically your answer might be better.
0

You must make an extra array for this, try this code;

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

    }  

And try to dump that again. You will now have 3 arrays inside DECLARED_FAULT and the other ones

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.