我是靠谱客的博主 呆萌芹菜,这篇文章主要介绍用foreach循环去取数据库的值,并合并数组,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
$list = $mTrans->alias('a') ->join('mpp_biz_time b','a.channel_id=b.id') ->field('a.down_sn,a.success_time,a.amount,a.fee,a.acc_no,b.title') ->where(['a.status' => '2']) ->where(['a.member_id' => $member_id]) ->where('a.success_time', 'between', [strtotime($mtime[0]), strtotime($mtime[1] . ' 23:59:59')]) ->order('a.success_time desc') ->select(); $list_all = json_decode(json_encode($list,JSON_UNESCAPED_UNICODE ),ture); if (empty($list)) { $this->EchoJson('0', '查询成功'); } foreach ($list_all as $k=>$v) { $sn[]=$v['sn']; $acc_no[]=$v['acc_no']; $info[] = Loader::model('admin/Cardbintb') ->field('bank_name,logo') ->where('card_len', strlen($v['acc_no'])) ->where('bin = SUBSTRING(:card_no, 1, bin_len)', ['card_no' => $v['acc_no']]) ->find()->toArray(); if (empty($temp[$v['date']])) { $temp[$v['date']]['total'] = $v['amount']; } else { $temp[$v['date']]['total'] += $v['amount']; } $mtotal += $v['amount']; } $arr = array(); foreach($list_all as $k=>$r){ $arr[] = array_merge($r,$info[$k]); }

数组类型$list_all

array(3) {

  [0]=>
  array(6) {
    ["down_sn"]=>
    string(24) "318031210214511700000823"
    ["success_time"]=>
    int(1520821445)
    ["amount"]=>
    string(6) "100.00"
    ["fee"]=>
    string(4) "4.42"
    ["acc_no"]=>
    string(16) "4581231314"
    ["title"]=>
    string(20) "银联无卡快捷T1"
  }
  [1]=>
  array(6) {
    ["down_sn"]=>
    string(24) "318031210200963800000822"
    ["success_time"]=>
    int(1520821269)
    ["amount"]=>
    string(6) "100.00"
    ["fee"]=>
    string(4) "4.42"
    ["acc_no"]=>
    string(16) "4581231314"
    ["title"]=>
    string(20) "银联无卡快捷T1"
  }
  [2]=>
  array(6) {
    ["down_sn"]=>
    string(24) "318031209553686800000821"
    ["success_time"]=>
    int(1520820954)
    ["amount"]=>
    string(6) "100.00"
    ["fee"]=>
    string(4) "4.42"
    ["acc_no"]=>
    string(16) "4581231314"
    ["title"]=>
    string(20) "银联无卡快捷T1"

  }

}

循环取数据库的值$info

array(3) {
  [0]=>
  array(2) {
    ["bank_name"]=>
    string(12) "交通银行"
    ["logo"]=>
    int(5)
  }
  [1]=>
  array(2) {
    ["bank_name"]=>
    string(12) "交通银行"
    ["logo"]=>
    int(5)
  }
  [2]=>
  array(2) {
    ["bank_name"]=>
    string(12) "交通银行"
    ["logo"]=>
    int(5)
  }
}

最终数据合集

{
    "errcode": 0,
    "msg": "查询成功",
    "data": {
        "list": [
            {
                "down_sn": "318031210214511700000823",
                "success_time": 1520821445,
                "amount": "100.00",
                "fee": "4.42",
                "acc_no": "4581231314",
                "title": "银联无卡快捷T1",
                "bank_name": "交通银行",
                "logo": 5
            },
            {
                "down_sn": "318031210200963800000822",
                "success_time": 1520821269,
                "amount": "100.00",
                "fee": "4.42",
                "acc_no": "4581231314994443",
                "title": "银联无卡快捷T1",
                "bank_name": "交通银行",
                "logo": 5
            },
            {
                "down_sn": "318031209553686800000821",
                "success_time": 1520820954,
                "amount": "100.00",
                "fee": "4.42",
                "acc_no": "4581231314",
                "title": "银联无卡快捷T1",
                "bank_name": "交通银行",
                "logo": 5
            }
        ],
    }
}

最后

以上就是呆萌芹菜最近收集整理的关于用foreach循环去取数据库的值,并合并数组的全部内容,更多相关用foreach循环去取数据库内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(74)

评论列表共有 0 条评论

立即
投稿
返回
顶部