Last active
December 17, 2015 14:39
-
-
Save zhoumengkang/5626451 to your computer and use it in GitHub Desktop.
把查询出来的数据还原为关联形式的树形结构展示
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include '../../nb.php'; | |
include '../../config.php'; | |
include 'config.php'; | |
include 'do.php'; | |
$db=\publish\obj::db(); | |
$year = $_GET['year']; | |
$sql = 'select a.year,a.guid,a.description,b.description as pic_des,c.year as pic_year ,c.month as pic_month,c.day as pic_day,c.file as pic_file from m_honor2_honor2 a, m_honor2_pics b, sys_file c where a.year = '.$year.' and b.honor2_guid = a.guid and b.image= c.guid order by a.order_is asc'; | |
//$sql = 'select year,description from m_honor2_honor2 where year = '.$year.' order by order_is asc'; | |
\nb\db\pdo::instance ( $db[0],$db[1] ); | |
$honor = \nb\db\pdo::execute('rows', $sql,null,$db[1]); | |
/********************************************** | |
这里多次关联查询之后得到的数组是这样的: | |
因为这里是a表中一条数据关联多条b表中的数据 | |
而在查询出来之后的情况为a表中的数据会重复输出,所以需要把数组修改完善为关联关系清晰的数组 | |
方法太烂,还没想到更好的办法,求指点 | |
Array | |
( | |
[0] => Array | |
( | |
[year] => 2005 | |
[guid] => 519c655c0a | |
[description] => x | |
[pic_des] => | |
[pic_year] => 2013 | |
[pic_month] => 05 | |
[pic_day] => 22 | |
[pic_file] => 519c65c90a.jpg | |
) | |
[1] => Array | |
( | |
[year] => 2005 | |
[guid] => 519c655c0a | |
[description] => x | |
[pic_des] => | |
[pic_year] => 2013 | |
[pic_month] => 05 | |
[pic_day] => 22 | |
[pic_file] => 519c66050a.jpg | |
) | |
[2] => Array | |
( | |
[year] => 2005 | |
[guid] => 519c655c0a | |
[description] => x | |
[pic_des] => | |
[pic_year] => 2013 | |
[pic_month] => 05 | |
[pic_day] => 22 | |
[pic_file] => 519c66100a.jpg | |
) | |
[3] => Array | |
( | |
[year] => 2005 | |
[guid] => 519c663a0a | |
[description] => y | |
[pic_des] => | |
[pic_year] => 2013 | |
[pic_month] => 05 | |
[pic_day] => 22 | |
[pic_file] => 519c66440a.jpg | |
) | |
[4] => Array | |
( | |
[year] => 2005 | |
[guid] => 519c663a0a | |
[description] => y | |
[pic_des] => | |
[pic_year] => 2013 | |
[pic_month] => 05 | |
[pic_day] => 22 | |
[pic_file] => 519c664d0a.jpg | |
) | |
[5] => Array | |
( | |
[year] => 2005 | |
[guid] => 519c663a0a | |
[description] => y | |
[pic_des] => | |
[pic_year] => 2013 | |
[pic_month] => 05 | |
[pic_day] => 22 | |
[pic_file] => 519c66570a.jpg | |
) | |
) | |
最后得到的数组: | |
Array | |
( | |
[0] => Array | |
( | |
[year] => 2005 | |
[guid] => 519c655c0a | |
[description] => x | |
[pic] => Array | |
( | |
[0] => Array | |
( | |
[guid] => 519c655c0a | |
[pic_des] => | |
[pic_year] => 2013 | |
[pic_month] => 05 | |
[pic_day] => 22 | |
[pic_file] => 519c65c90a.jpg | |
) | |
[1] => Array | |
( | |
[guid] => 519c655c0a | |
[pic_des] => | |
[pic_year] => 2013 | |
[pic_month] => 05 | |
[pic_day] => 22 | |
[pic_file] => 519c66050a.jpg | |
) | |
[2] => Array | |
( | |
[guid] => 519c655c0a | |
[pic_des] => | |
[pic_year] => 2013 | |
[pic_month] => 05 | |
[pic_day] => 22 | |
[pic_file] => 519c66100a.jpg | |
) | |
) | |
) | |
[3] => Array | |
( | |
[year] => 2005 | |
[guid] => 519c663a0a | |
[description] => y | |
[pic] => Array | |
( | |
[0] => Array | |
( | |
[guid] => 519c663a0a | |
[pic_des] => | |
[pic_year] => 2013 | |
[pic_month] => 05 | |
[pic_day] => 22 | |
[pic_file] => 519c66440a.jpg | |
) | |
[1] => Array | |
( | |
[guid] => 519c663a0a | |
[pic_des] => | |
[pic_year] => 2013 | |
[pic_month] => 05 | |
[pic_day] => 22 | |
[pic_file] => 519c664d0a.jpg | |
) | |
[2] => Array | |
( | |
[guid] => 519c663a0a | |
[pic_des] => | |
[pic_year] => 2013 | |
[pic_month] => 05 | |
[pic_day] => 22 | |
[pic_file] => 519c66570a.jpg | |
) | |
) | |
) | |
) | |
***********************************************/ | |
$honor2=$honor; | |
$honor3=$honor; | |
foreach ($honor2 as $k => $v) { | |
if($honor2[$k]['guid']==$honor2[$k+1]['guid']){ | |
$flag[]=$k+1; | |
} | |
} | |
foreach ($flag as $k => $v) { | |
unset($honor2[$v]); | |
} | |
foreach($honor2 as $k=>&$v){ | |
unset($v['pic_des']); | |
unset($v['pic_year']); | |
unset($v['pic_month']); | |
unset($v['pic_day']); | |
unset($v['pic_file']); | |
} | |
foreach($honor3 as $k=>&$v){ | |
unset($v['year']); | |
unset($v['description']); | |
} | |
foreach($honor2 as $k=>&$v){ | |
foreach($honor3 as $kk=>$vv){ | |
if($honor2[$k]['guid'] == $honor3[$kk]['guid']){ | |
$honor2[$k]['pic'][] =$vv; | |
} | |
} | |
} | |
\nb\log($honor2); | |
echo json_encode($honor2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment