• PHP多表取数据的代码优化


    <?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] => 天猫商城
            )
    
    )
    
    
    
    */
    ?>
  • 相关阅读:
    【CodeForces】835D Palindromic characteristics
    【BZOJ】2006: [NOI2010]超级钢琴
    【比赛】STSRM 09
    【比赛】洛谷夏令营NOIP模拟赛
    【BZOJ】4147: [AMPPZ2014]Euclidean Nim
    【BZOJ】3895: 取石子
    【胡策08】解题报告
    【codevs】3196 黄金宝藏
    【BZOJ】1443: [JSOI2009]游戏Game
    【BZOJ】3105: [cqoi2013]新Nim游戏
  • 原文地址:https://www.cnblogs.com/rxbook/p/6008879.html
Copyright © 2020-2023  润新知