duanjue2560 2015-11-09 12:10
浏览 19
已采纳

需要从mysql中的2个表中获取数据

i have two tables filter_categories and filter tags

filter_categories
id | category 

filter_tags
id | filter_category_id | filter_tag

i m writing following query to join these tables

"select filter_categories.filter_category,
filter_tags.filter_tag from filter_categories JOIN filter_tags ON 
(filter_tags.filter_category_id=filter_categories.id) where   
 filter_categories.filter_id='".$_GET["id"]."' 

i m getting this result with php print_r()

Array
(
[0] => Array
    (

        [filter_category] => abcty
               [filter_tag] => sdfds

    )

[1] => Array
    (

        [filter_category] => abcty

        [filter_tag] => dgdfg

    )

[2] => Array
    (

        [filter_category] => abcty

        [filter_tag] => gdgdf

    )

[3] => Array
    (

        [filter_category] => abcty

        [filter_tag] => dgdfgf

    )

[4] => Array
    (


        [filter_category] => abcty
        [filter_tag] => dsfs

    )

)

But i need to fetch result like this

 Array
(
[0] => Array
    (

        [filter_category] => abcty
        [filter_tag] => array(
        [filter_tag] => dgdfg
        [filter_tag] => dgdfgf
        [filter_tag] => dsfs
)
)
)

[1] => Array
    (

        [filter_category] => efg
       [filter_tag] => array(

  [filter_tag] => et
[filter_tag] => yu
 [filter_tag] => op
    )
    )
   ) 

can someone help me out..how to do it? Thanks

  • 写回答

1条回答 默认 最新

  • dsnw2651 2015-11-09 12:21
    关注

    First of all modify your query like this to get the id of category

    select filter_categories.id,filter_categories.filter_category,
    filter_tags.filter_tag from filter_categories JOIN filter_tags ON 
    (filter_tags.filter_category_id=filter_categories.id) where   
     filter_categories.filter_id='".$_GET["id"].
    

    then suppose your result are coming into $arrfilterALLDetails array then just use the for loop to modify the result as want above like below.

     $arrfilterDetails = array();
     foreach ($arrfilterALLDetails as $key => $value) {
              $arrfilterDetails[$value['id']]['filter_category'] = $value['filter_category'];
              $arrfilterDetails[$value['id']][$value['filter_tag']] = $value['filter_tag'];
            }
    print_r($arrfilterDetails);
    

    Just play with this a littlebit you will get the result as you want.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)