Currently running Laravel 4.1.31. In the controller an array is built of users with their id as the key:
$owners = User::get()->lists('username','id');
Printing the owners array out at the controller level would produce the following:
print_r($owners)
// Array
// (
// [1] => user1
// [2] => user2
// [4] => user3 <--- key jumps to 4, this is correct
// [5] => user4
// [6] => user5
// )
However once the array is passed to a view the keys are not preserved, printing it out at the view level produces the following:
// Array
// (
// [1] => user1
// [2] => user2
// [3] => user3 <--- key was replaced with 3, this is incorrect
// [4] => user4 all values from this point on are now shifted
// [5] => user5
// )
There was no 3 key in the original array however once it was passed to the view everything shifted to fill in the gap. This ends up causing problems as all id's above 2 are now mismatched. How can the arrays keys be preserved?
Full controller method:
public function edit($id) {
// get the task
$task = $this->task->find($id);
// grab all users for owner field
$owners = User::get()->lists('username','id');
// grab all projects for project field
$projects = Project::get()->lists('title','id');
// add placeholder to beginning of arrays
array_unshift($owners, 'Select Owner');
array_unshift($projects, 'Select Project');
// return show view
return View::make('tasks.edit', array(
'task' => $task,
'status' => $this->status,
'projects' => $projects,
'owners' => $owners
));
}
$owners = User::select("username", "id")->distinct()->get();?dd($owners)before thearray_unshiftand note the results. Then do one afterarray_unshift, do the keys change then or only when sent totasks.edit?array_unshiftis not preserving the keys.