繁体   English   中英

获取二维数组中的数据

[英]Get the data which in two dimensional array

您好,这可能看起来很简单,但是我被击中了...在我的应用程序中,我得到了一个像

Array
(
[0] => Array
    (
        [home_id] => 1
        [distance] => 12
    )
[1] => Array
    (
        [home_id] => 3
        [distance] => 14
    )


[2] => Array
    (
        [home_id] => 94
        [distance] => 1.679713069
    )
    .
    .
    .
    .

)

我的桌子看起来像

home_id  | home_name

1        |   My home 1

2        |   My home 2

3        |   My home 3

从该数组中,我将获得数据库表中的home_id。 所以我如何获得结果的详细信息,其中包括home_name和到第一个数组的距离,可能像

home_name        |  distance
 ___________________________
My home 1        |   0.562620830044

My home 3        |   14

先感谢您

使用codeigniter active record查询遍历数组并从数据库获取home_name

foreach($yourArray as $row)
{
   $id = $row['home_id'];
   $distance = $row['distance'];
   $db_result = $this->db->get_where('yourtable', array('home_id' => $id));
   if($db_result && $db_result->num_rows() > 0){
    $result = $db_result->row();
    $home_name = $result->home_name;
   }

}

如果您不能在一个查询中JOIN两个表,而必须使用该数组,则可以执行以下操作:

foreach($yourArray as $home)
{
   $id=$home["home_id"];
   $distance=$home["distance"];
   $id=intval($id);
   $sql="SELECT home_name FROM yourTable WHERE home_id=$id";   // execute this query to get name
}
From this array i will get the home_id which is in database table. So How can i get the result details which includes the home_name and the distance from the first array which might be like

如果要使用从主数组获取的id从表home_details_table获取home detailshome_details_tablehome_details_table替换home_name中的home_details_table home_id ,并将两个表链接One to Many关系。

home_table:

home_id  | home_name
1        |   My home 1

2        |   My home 2

3        |   My home 3

home_detail_table:

home_id  |  distance
1        |   0.562620830044

3        |   14

然后使用JOIN ,您将可以执行以下操作:

foreach($mainArray as $home)
{
   $id = $home["home_id"];
   $sql="SELECT h.home_id, h.home_name, d.home_distance FROM home_table h JOIN home_details_table d ON h.home_id = d.home_id WHERE h.home_id = ".$id;
   // with this query you will have the name and distance of the given home id.

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM