I have some code that grabs some rows from a table and I want to make an associative array in PHP that will be like [row_id] => row_title. This will then be used to populate a select list. The problem is, the key, unless I include a string in it, always reverts to an ordered number starting with 0. If I add a string in with the key, then the key retains the ID that I want. But obviously I don't want the string in the key.
Here's my code:
public static function select_array(){
$courses = ORM::factory('course')->order_by('title')->find_all();
$out = array();
foreach($courses as $c){
$out[$c->id] = $c->title;
}
return $out;
}
The $c->id should be the id from the database, but PHP is overriding this with a count number. If I do this:
public static function select_array(){
$courses = ORM::factory('course')->order_by('title')->find_all();
$out = array();
foreach($courses as $c){
$test = "." . $c->id;
$out[$test] = $c->title;
}
return $out;
}
Then the key will include the correct ID, but with a "." in front... I've tried using (string)$c->id but no luck with that either. I've also tried setting $test as " " . $c->id and then trimming it, but that doesn't work, and it's just a weird hacky attempt anyway.
Hope that makes sense. My question: How to simply tell the key to be a specific number?