如何使用Id和Value创建关联数组?

I have a table that contains id and weight. I want to put both in an associative array.

Right now I've managed to put only the weight in an array:

$weight= array();
$stmt = $dbc->query("SELECT * FROM tbl_weight");
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
 $weight[] = $row['weight'];
}

I want to also put the id and make it an associative array.

For example for id = 1 and weight = 50, I want to be able to do something like:

$weight = array("1"=>"50");

How can I do this?

Just set your key when you append the value. As long as id is unique you shouldn't have any problems

$weight= array();
$stmt = $dbc->query("SELECT * FROM tbl_weight");
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $weight[$row['id']] = $row['weight'];
}

You'll want to do

$weight[$row['id']] = $row['weight'];