如何使用in_array()函数创建数组?

时间:2014-11-12 14:30:23

标签: php arrays multidimensional-array mysqli associative-array

我遇到处理数组的问题。我想“比较”两个数组,以查看两个数组中是否存在匹配的“用户名”。我不确定我是否正确使用in_array()函数

这就是我的数组的样子:

USER array 1:

 Array ( [0] => Array ( [username] => LNDP [station] => D08 ) 
        [1] => Array ( [username] => ACMAN [station] => D06 )
        [2] => Array ( [username] => VTER [station] =>  D13 )
      )

    //the users will have to be matched with memo_code
    $user = array();
    while($row = mysqli_fetch_assoc($get_user_result)){
        $user[] = array( "username" => $row['username'],
                         "station"  =>  $row['station_number']                                                          
                            );
    }

备忘录阵列2:

 Array (   [0] => Array ( [username] => VTER[aht_value] =>  333 ) 
           [1] => Array ( [username] => ACMAN [aht_value] => 456 ) 
           [2] => Array ( [username] => ACYL [aht_value] =>  789 )
         )


$memo = array();
    while ($row = mysqli_fetch_assoc($dbh2_result)){   
        $memo[] = array( "username"  => $row['memo_code'],
                       "aht_value" => $row['avg_handle_time']
                      );
    }

我想检查MEMO数组中的每个“用户名”以匹配USER数组中的“用户名”。 如果它们匹配,我想创建一个带有username,station,aht_value的数组,如下所示:

Array ( [0] => Array ( [username] => ACMAN [aht_value] => 456 [station] => D06 ) 
      )

我尝试了什么:

//通过使用数组2中“username”的键值比较1和2来创建数组3         $ result = array();         // $ m =键         // $ m ['username'] =键值         foreach($ memo as $ m => $ m ['username']){

        //if username in array 2 is in array 1
        if( in_array( $m, $user) ){
            //if aht_value is not null
            if( $memo['aht_value'] != null ){
            $result[] =  "username: " . $user['username'] . "aht_value: " .$m['aht_value'] . "position: " . $user['position']. "<br>";   
            }
            //else if aht_value is null
            else{
                $result[] = "username: " . $user['username'] . "aht_value: NA  position: " . $user['position'] . "<br>";
            }
        }
        //if there is no matching username do something?
        else{
            echo "no match";
        }

    }

    $final_result = json_encode($result);
    echo $final_result;

如果我需要澄清一些事情,请询问。在成功创建第三个数组后,我将使用json_encode进行AJAX调用并使用类型GET。

提前感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

$memo = array();
$result = array();
$user = array();

while($row = mysqli_fetch_assoc($get_user_result)) {
    $user[] = array(
        "username" => $row['username'],
        "station"  =>  $row['station_number']
    );
}
while ($row = mysqli_fetch_assoc($dbh2_result)) {   
    $memo[] = array(
        "username"  => $row['memo_code'],
        "aht_value" => $row['avg_handle_time']
    );
}

foreach ($memo as $i => $memodata) {
    foreach ($user as $j => $userdata) {
        if ($memodata['username'] == $userdata['username']) {
            if (is_null($memodata['aht_value'])) {
                $result[] = "username: " . $userdata['username'] . " aht_value: NA  position: " . $userdata['station'];
            } else {
                $result[] =  "username: " . $userdata['username'] . " aht_value: " .$memodata['aht_value'] . " position: " . $userdata['station'];   
            }
        } 
    }
}


echo json_encode($result);

这种方法效率不高,因为你必须遍历两个数组。 SQL JOIN可能更好,或者您可以向数组添加UNIQUE索引。 (如果重复用户名,这将无法工作,因为关联数组不能有重复的键)

<?php

$memo = array();
$result = array();
$user = array();

while($row = mysqli_fetch_assoc($get_user_result)) {
    $user[$row['username']] = array(
        "username" => $row['username'],
        "station"  =>  $row['station_number']
    );
}
while ($row = mysqli_fetch_assoc($dbh2_result)) {   
    $memo[$row['memo_code']] = array(
        "username"  => $row['memo_code'],
        "aht_value" => $row['avg_handle_time']
    );
}

foreach ($memo as $username => $memodata) {
    if (in_array($username, array_keys($user))) {
        // Match username against the keys of $user (the usernames) */
        $userdata = $user[$username];
        if (is_null($memodata['aht_value'])) {
            $result[] = "username: " . $userdata['username'] . "aht_value: NA  position: " . $userdata['position'] . "<br>";
        } else {
            $result[] =  "username: " . $userdata['username'] . "aht_value: " .$m['aht_value'] . "position: " . $userdata['position']. "<br>";   
        }
    }
}


echo json_encode($result);

答案 1 :(得分:0)

可能有更好的方法,这应该达到你想要的效果:

$data = array()
foreach($memo as $m){
    foreach($user as $u){
        if($m['username']===$u['username']){
            $m['station'] = $u['station'];
            $data[] = $m;
        }
    }
}

但如果我是你,我会设法在用户名或id上使用SQL JOIN合并记录,以便只执行一个查询。

答案 2 :(得分:0)

尝试这样的事情:

<?php   

    function inUserArray($userArray, $username)
    {   
        foreach($userArray as $user)
        {
            if($user['username'] == $username)
            {
                return $user;
            }
        }

        return array();
    }


    foreach($memo as $m){

        //if username in array 2 is in array 1
        $user = inUserArray($user, $m);
        if( !empty($user) ){
            //if aht_value is not null
            if( $m['aht_value'] != null ){
            $result[] =  "username: " . $user['username'] . "aht_value: " .$m['aht_value'] . "position: " . $user['position']. "<br>";   
            }
            //else if aht_value is null
            else{
                $result[] = "username: " . $user['username'] . "aht_value: NA  position: " . $user['position'] . "<br>";
            }
        }
        //if there is no matching username do something?
        else{
            echo "no match";
        }

    }          
?>