Arr"/> Arr">
Home > Backend Development > PHP Tutorial > mysql查询出的数据显示方式解决办法

mysql查询出的数据显示方式解决办法

WBOY
Release: 2016-06-13 13:14:31
Original
773 people have browsed it

mysql查询出的数据显示方式

$sql="SELECT c.cat_name,a.title FROM hc_article as a LEFT JOIN hc_article_cat AS c ON a.cat_id = c.cat_id";
用以上语句查询出

Array
(
  [0] => Array
  (
  [cat_name] => 新手指南
  [title] => 用户注册
  )

  [1] => Array
  (
  [cat_name] => 新手指南
  [title] => 购物指南
  )




问 怎么可以排列成  


Array
(
  [0] => Array
  (
  [cat_name]=>Array( [0] => 新手指南) 
  [title]=>Array( [0] => 用户注册 [1] =>购物指南) 
 
  )

 )

------解决方案--------------------

PHP code
$ar=Array
(
  '0' => Array
  (
  'cat_name' => '新手指南',
  'title' => '用户注册'
  ),

  '1' => Array
  (
  'cat_name' => '新手指南',
  'title' => '购物指南'
  )
);
foreach($ar as $v){
     if(!$t[$v['cat_name']]){
            $t[$v['cat_name']]['cat_name']=array($v['cat_name']);
           $t[$v['cat_name']]['title']=array($v['title']);
     }       
     else $t[$v['cat_name']]['title'][]=$v['title'];
}

print_r(array_values($t)); <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template