Log in

View Full Version : convert Results into Associative array



hemi519
02-15-2013, 08:21 AM
Hi All,

I want to convert mysql results into associative array like below


function jobs() // previously i was using this function and i was declaring as mentioned in $job_arr
{

$job_arr = array(
' ' => "Please select job type",
'1'=>"Full Time",
'2'=>"Part Time",
' 3' =>"Freelancing"
);

return $job_arr;
}

But i want to get these results from my mysql result


function jobs()
{


$sql = "Select * from jh_job_type where Status = 1";
$query = $ci->db->query($sql);
$result = $query->result_array();

// Id,Name are the field names

$job_arr = ''; // i started doing with foreach but did not get required result.



return $job_arr;
}


Anyone know how to do this one?

hemi519
02-15-2013, 03:35 PM
solved :)

james438
02-15-2013, 03:38 PM
Good to hear! How did you solve it? If it is solved could you mark this thread as resolved?

hemi519
02-19-2013, 06:38 AM
I have changed my function as below



function jobs()
{


$sql = mysql_query("Select Id,Name from jh_job_type where Status = 1");
while($result = mysql_fetch_array($sql))
{
$jobs_arr[$result['Id']] = $result['Name'];
}
return $jobs_arr;

}


This solved my issue. Hope it helps someone

fastsol1
02-19-2013, 11:34 PM
Why not just use mysql_fetch_assoc instead?