使用id作为引用的数组连接

时间:2014-10-14 20:46:08

标签: php mysql arrays

我有2张桌子。第一个表是设备表。此表中的查询如下所示:

id    name      user_id
-----------------------
1     equip1    1001
2     equip2    1002

seconde表是用户表。此表中的查询如下所示:

id     username
--------------
1001   user1
1002   user2

我想实现这样的目标:

id    name      user_id    username
-----------------------------------
1     equip1    1001       user1
2     equip2    1002       user2

有没有办法像连接查询一样加入两个数组?我不能在查询中使用JOIN,因为表位于不同的数据库上(我知道有一种方法可以在不同的数据库上进行JOIN,但我不允许使用它)。

编辑:

我正在添加这些数组的结构。

$equipment = array( 
                [0] => array( 
                    ['id'] => 1, 
                    ['name'] => 'equip1', 
                    ['user_id'] => 1001 
                ),
                [1] => array( 
                    ['id'] => 2, 
                    ['name'] => 'equip2', 
                    ['user_id'] => 1002
                ) 
             );

$users= array( 
                [0] => array( 
                    ['id'] => 1001, 
                    ['username'] => 'user1'
                ),
                [1] => array( 
                    ['id'] => 1002, 
                    ['username'] => 'user2'
                ) 
             );

1 个答案:

答案 0 :(得分:2)

您可能需要自己加入查询。我不相信有内置函数(不计算带回调的walk或map)。这就是我要做的事情

//empty array for indexing users under their id for faster loopups
$users = array();

//loop over the users result
foreach($usersResult as $row){
    //index users under their id.
    $users[$row['id']] = $row['username'];
}

//now loop over the equipment to join the arrays together
foreach($equipmentResult as $key=>$row){
    //add the username column
    $row['username'] = isset($users[$row['user_id']])?$users[$row['user_id']]:null;

    //save back into the equipment row
    $equipmentResult[$key] = $row;
}

//display
print_r($equipmentResult);

这很容易变成一个函数,你传递的参数会为列名构建“ON”部分。

编辑:使其成为一种功能。

<?php

/**
 * Joins two arrays as if they were joined in a query
 * @param  Array   $arrayA   The base (left) array to join into
 * @param  Array   $arrayB   The right array to join into A
 * @param  String  $colA     The column name to join on for arrayA
 * @param  String  $colB     [optional] The column name to join on for arrayB. If 
 *                           blank, then it is assumed the same column name as colA
 * @param  boolean $leftJoin [optional] Should this be a left join and include rows
 *                           from A where no value exists in B?
 * @return void
 */
function array_join($arrayA, $arrayB, $colA, $colB=null, $leftJoin=false){
    //if no value was passed for colB, assume it is the same value as colA
    if(is_null($colB)){
        $colB = $colA;
    }

    //output data
    $out = array();

    //create an index for array B for faster lookups
    $idxB = array();
    $colsB = array();
    foreach($arrayB as $row){
        //get the value from B
        $valB = $row[$colB];
        //if the column doesn't exist in the index, add it
        if(!isset($idxB[$colB])){
            $idxB[$colB] = array();
        }
        //index the value
        $idxB[$valB][] = $row;

        //store the known column to an array for use below
        if(empty($colsB)){
            $colsB = array_keys($row);
        }
    }

    //loop over array A
    foreach($arrayA as $rowA){
        //get the value for the column
        $valA = $rowA[$colA];

        //does the value from A exist in B
        $rowB = isset($idxB[$valA])?$idxB[$valA]:null;

        //join the rows
        //add blank columns if left join
        if($leftJoin && is_null($rowB)){
            $rowBJoin = array_combine($colsB, array_fill(0, count($colsB), null));
            //add the row to our output
            $out[] = $rowA + $rowBJoin;
        } else {
            //inner join or value is not null
            //loop over all the rows from the B index that we are joining on
            foreach($rowB as $rowBJoin){
                //add the row to our output
                $out[] = $rowA + $rowBJoin;
            }
        }
    }

    return $out;
}