views:

118

answers:

1

i'm having some trouble trying to use a query result as an array

First i run a query to get all the zips that i need

   $this->db->select('zip');
    $this->db->from('custom_city');
    $this->db->join('city_to_zip', 'custom_city.id = city_to_zip.city_id', 'left');
    $this->db->where('city_to_zip.city_id', $_POST['city']);
    $zip = $this->db->get();
    $data['zips'] = $zip;

    $zip_array = $zip->result_array();

then the query output is this, when i try to use it.

Now i know it's doing something, because it says "array" 8 times and thats the right count for the query. I just need to know how to get the zip code in there instead of the word array.

AND `zip_code` IN (Array, Array, Array, Array, Array, Array, Array, Array) [/quote]

in my query i'm using...

 $this->db->where_in('zip_code', $zip_array);

Thanks,

Jbeasley

+3  A: 

This is because result_array() returns an associative array for each row (so you're getting an array of arrays). To get the zip codes into $zip_array, replace this line:

$zip_array = $zip->result_array();

with

$zip_array = array();
$result = $zip->result_array();
foreach ($result as $row) {
    $zip_array[] = $row['zip'];
}

Documentation on result_array()

jimyi