代码之家  ›  专栏  ›  技术社区  ›  Khaled

连接两个多维数组

  •  0
  • Khaled  · 技术社区  · 8 年前

    我有两个数组,每个数组来自不同的数据库查询,我不能在数据库中加入它们,所以我必须使用php来加入它们。 第一个:

    Array
    (
        [0] => Array
            (
                [id] => 23
                [host_id] => 5
                [pos_id] => 2
                [status] => 1
            )
    
        [1] => Array
            (
                [id] => 25
                [host_id] => 5
                [pos_id] => 1
                [status] => 1
            )
    
        [2] => Array
            (
                [id] => 24
                [host_id] => 5
                [pos_id] => 2
                [status] => 1
            )
    
    )
    

    我想加入这个阵型 pos_id id

    Array
    (
        [0] => Array
            (
                [id] => 1
                [name] => Front
            )
    
        [1] => Array
            (
                [id] => 2
                [name] => Back
            )
    )
    

    因此,结果是:

    Array
    (
        [0] => Array
            (
                [id] => 23
                [host_id] => 5
                [pos_id] => 2
                [name] => Back
                [status] => 1
            )
    
        [1] => Array
            (
                [id] => 25
                [host_id] => 5
                [pos_id] => 1
                [name] => Front
    
                [status] => 1
            )
    
        [2] => Array
            (
                [id] => 24
                [host_id] => 5
                [pos_id] => 2
                [name] => Back
                [status] => 1
            )
    
    )
    

    我的代码使用一个内部循环,匹配值并推入数组:

    foreach($events as &$event) {
        foreach($positions as $position) {
            if($player[pos_id] == $position[id]){
                array_push($event,"name",$position[name]);
            }
        }
    }
    
    0 回复  |  直到 8 年前