array_map的多個數組技巧

# 獲取家列表
$home_list = Model::home_users()->selectBySql(
    'select
    h.home_id,h.name
    from home_users as hu left join home as h on hu.home_id = h.home_id
     where hu.user_id=?'
    ,[$this->token_info['id']]
);
輸出:
 [
        ['home_id'=>'1','count'=>'31'],
        ['home_id'=>'2','count'=>'24']
    ];
   
# 家的消息數目
$msg = self::readMsg([
    'user_id'=>$this->token_info['id'],
    'home_id'=>$homes,
    'read_type'=>'r'
]);
返回:
 [
        ['home_id'=>'1','count'=>'31'],
        ['home_id'=>'2','count'=>'24']
    ];

$new = array_map(function($h,$m){
    return array_merge($h,$m);
},$home_list,$msg);
返回:
Array
(
    [0] => Array
        (
            [home_id] => 1
            [home_name] => 家1
            [count] => 31
        )

    [1] => Array
        (
            [home_id] => 2
            [home_name] => 家2
            [count] => 24
        )

)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章