数组问题,以及where子句

时间:2021-07-24 07:15:36

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.

现在我知道它正在做一些事情,因为它说“阵列”8次,这是查询的正确计数。我只需要知道如何在那里获取邮政编码而不是单词数组。

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

1 个解决方案

#1


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:

这是因为result_array()为每一行返回一个关联数组(所以你得到一个数组数组)。要将邮政编码放入$ zip_array,请替换此行:

$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()

关于result_array()的文档

#1


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:

这是因为result_array()为每一行返回一个关联数组(所以你得到一个数组数组)。要将邮政编码放入$ zip_array,请替换此行:

$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()

关于result_array()的文档