php合并数组array_merge函数应用实例

今天工作中碰到一个需要是先查指定数据库中是不是有满足条件的6条数据,如果没有再从B表中查找 6-A表中的数据之后再进行数组合并了,下面我来把我们整个过程给各位分享一下.

在php中合并数组我们可以使用array_merge函数

array array_merge (array array1 array2…,arrayN)

PHP实例代码如下:

  1. //团购1
  2. $t=1;
  3. $tugou = esf(" * "," setindex='1' $cwhere order by id desc limit 0,6","A表");
  4. foreach( $tugou as $v =>$_v )
  5. {
  6. $tugou[$v]['i'] =$t++;
  7. }
  8. $abpccount = count( $tugou );
  9. //echo $abpccount;
  10. if( $abpccount < 6 )
  11. {
  12. $page->tpl->assign('notg',1);//调用团购标签
  13. $abpclimit = 6 - $abpccount;
  14. $tugou_loupan = esf(" * "," index_dt='1' $cwhere order by id desc limit 0,$abpclimit ","B表");
  15. foreach( $tugou_loupan as $v =>$_v )
  16. {
  17. $tugou_loupan[$v]['i'] =$t++;
  18. $tugou_loupan[$v]['abpcindexpic'] = $_v['abpcthumb'];
  19. $tugou_loupan[$v]['abpclitpic'] = $_v['abpcthumb'];
  20. $tugou_loupan[$v]['abpcname'] = mt_rand(10,80);
  21. $tugou_loupan[$v]['abpcdown'] = 'xx 惠';
  22. $tugou_loupan[$v]['abpclpname'] = $_v['abpcname'];
  23. }
  24. $tugou = array_merge($tugou,$tugou_loupan);
  25. }
  26. $page->tpl->assign('tugou',$tugou);

注意事项:array_merge()合并时我们必须要注意一点的是中间的值必须为数组否则合并之后会返回空.