码迷,mamicode.com
首页 > Web开发 > 详细

PHP多表取数据的代码优化

时间:2016-10-28 19:54:40      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:语句   取数   需要   each   数据   style   color   _id   sel   

<?php

header("Content-type: text/html; charset=utf-8");

//假设这里的$goods_arr  和 $shop_arr  都是从数据库中查询出来的数组
$goods_arr = array(
          0 => array(
               ‘activity_id‘ => 1,
               ‘activity_name‘ => ‘午夜今日特价345345sss‘,
               ‘shop_id‘ => 300001,
          ),
          1 => array(
                    ‘activity_id‘ => 2,
                    ‘activity_name‘ => ‘0点档限量抢购‘,
                    ‘shop_id‘ => 300002,
          ),
          2 => array(
                    ‘activity_id‘ => 3,
                    ‘activity_name‘ => ‘凌晨惊爆抢购‘,
                    ‘shop_id‘ => 300003,
          ),
          3 => array(
                    ‘activity_id‘ => 4,
                    ‘activity_name‘ => ‘午夜限时抢购‘,
                    ‘shop_id‘ => 300003,
                    ‘create_time‘ => 1395050666,
                    ‘update_time‘ => 1411887987,
                    ‘is_del‘ => 1,
          ),
          4 => array(
                    ‘activity_id‘ => 5,
                    ‘activity_name‘ => ‘早场限量秒杀‘,
                    ‘shop_id‘ => 300001,
          ),
          5 => array(
                    ‘activity_id‘ => 6,
                    ‘activity_name‘ => ‘9点底价抢购‘,
                    ‘shop_id‘ => 300002,
          )
);

$shop_arr = array(
          0 => array(
                    ‘shop_id‘ => 300001,
                    ‘shop_name‘ => ‘一号店‘,
          ),
          1 => array(
                    ‘shop_id‘ => 300002,
                    ‘shop_name‘ => ‘天猫商城‘,
          ),
          2 => array(
                    ‘shop_id‘ => 300003,
                    ‘shop_name‘ => ‘京东‘,
          ),
          3 => array(
                    ‘shop_id‘ => 300004,
                    ‘shop_name‘ => ‘当当网‘,
          )
);

//传统做法,需要循环查询 tbShop表 的 次数就是 count($goods_arr)
foreach($goods_arr as $k=>$v){
     $goods_arr[$k][‘shop_name‘] = "select shop_name from tbShop where shop_id = $v[shop_id]";//这里只是SQL语句,能够表达意思即可。
}

//重点开始....................
$temp = array();
foreach($shop_arr as $val){
     $temp[$val[‘shop_id‘]] = $val;
}
//通过引用传递 &$v 将key "shop_name" 付给 $goods_arr [精辟]
foreach($goods_arr as &$v){
     $shop_id=$v[‘shop_id‘];
     $v[‘shop_name‘]= $temp[$shop_id][‘shop_name‘];
}

print_r($goods_arr);


/**
//最后结果

Array
(
    [0] => Array
        (
            [activity_id] => 1
            [activity_name] => 午夜今日特价345345sss
            [shop_id] => 300001
            [shop_name] => 一号店
        )

    [1] => Array
        (
            [activity_id] => 2
            [activity_name] => 0点档限量抢购
            [shop_id] => 300002
            [shop_name] => 天猫商城
        )

    [2] => Array
        (
            [activity_id] => 3
            [activity_name] => 凌晨惊爆抢购
            [shop_id] => 300003
            [shop_name] => 京东
        )

    [3] => Array
        (
            [activity_id] => 4
            [activity_name] => 午夜限时抢购
            [shop_id] => 300003
            [create_time] => 1395050666
            [update_time] => 1411887987
            [is_del] => 1
            [shop_name] => 京东
        )

    [4] => Array
        (
            [activity_id] => 5
            [activity_name] => 早场限量秒杀
            [shop_id] => 300001
            [shop_name] => 一号店
        )

    [5] => Array
        (
            [activity_id] => 6
            [activity_name] => 9点底价抢购
            [shop_id] => 300002
            [shop_name] => 天猫商城
        )

)



*/
?>

 

PHP多表取数据的代码优化

标签:语句   取数   需要   each   数据   style   color   _id   sel   

原文地址:http://www.cnblogs.com/renxing123/p/6008879.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!