CodeIgniter Forums
Active record query result conversion - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Active record query result conversion (/showthread.php?tid=9886)



Active record query result conversion - El Forum - 07-11-2008

[eluser]kidego32[/eluser]
hi everyone,

I have a multidimensional query result array as follows:

$query_result = Array (
array (customer_id => 1, customer_name => name1)
array (customer_id => 2, customer_name => name2)
array (customer_id => 4, customer_name => name3))

I am trying to convert it to an single dimensional array to be used to populate a drop down box. This is my code so far:

Code:
for ($row = 0; $row < count($query_result); $row++)
{
   foreach($query_result[$row] as $key => $value)
      $options_array[$row+1] = $value;
}

This only works when customer_id is consecutive, but with the example above, it breaks down. I can't figure out how to store the customer_id value as the key to the new array, and the customer_name value as the value to the new key.

To put it another way: I need the new array's keys to be the customer_id values, and the new array's values to be the customer_name values.

I hope this makes at least a bit of sense ...


Active record query result conversion - El Forum - 07-11-2008

[eluser]Bramme[/eluser]
Code:
foreach($query_result as $row) {
$id = $row['customer_id'];
$name = $row['customer_name'];
$options_array[$id] = $name;
}
No?


Active record query result conversion - El Forum - 07-11-2008

[eluser]kidego32[/eluser]
Yes Bramme. Thank you. :-)