Reputation: 534
I am querying my postgres database and getting a name and id like so:
$datesQuery = "SELECT date_ridden, id from dates WHERE user_id=$userId"; //query
$theDates = pg_query($db, $datesQuery); //execute query
$dates=array(); //want to use this array to have the key as id and value as date_ridden
I want to make my $dates array with the id as the key and date_ridden as the value.
Currently I'm doing the following (which is not what I want to do):
while( $date = pg_fetch_row($theDates) ){
$dates[]['value'] = $date[1]; //date id is value
$dates[]['label'] = $date[0]; //date (in text form) is the label
}
I feel like this should be a really simple thing to do, but for some reason can't figure it out.
Upvotes: 0
Views: 454
Reputation: 214
If I understand what your asking, then it would be something like this
while( $date = pg_fetch_row( $theDates ) ) {
$dates[][$date[1]] = $date[0];
}
or if your wanting to access $dates['some id'] then
while( $date = pg_fetch_row( $theDates ) ) {
$dates[$date[1]] = $date[0];
}
Upvotes: 0
Reputation: 69967
I believe the loop structure you are looking for is:
while( $date = pg_fetch_row($theDates) ){
$dates[$date[1]] = $date[0];
}
If I misunderstood, then the issue with your current attempt is that it appends the id and label as separate array indexes in $dates
.
Instead try this:
while( $date = pg_fetch_row($theDates) ){
$dates[] = array('value' => $date[1],
'label' => $date[0]);
}
With this example, you would access an entry like Value = $dates[0]['value'], Label = $dates[0]['label']
Hope one of those helps.
Upvotes: 1