Welcome Guest, Not a member yet? Register   Sign In
Active record query result conversion
#1

[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 ...
#2

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

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




Theme © iAndrew 2016 - Forum software by © MyBB