如果两个数组中都存在值,则使用KEY创建一个数组? [英] Create an Array with a KEY if values exists in two arrays?

查看:157
本文介绍了如果两个数组中都存在值,则使用KEY创建一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理数组时遇到问题.我想比较"两个数组以查看两个数组中是否有匹配的用户名".我不确定我是否正确使用了in_array()函数

I am having problem with handling arrays. I want to "compare" two arrays to see if there's matching "usernames" in both arrays. I am not sure if I am using in_array() function properly

这是我的数组的样子:

用户数组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']                                                          
                            );
    }

MEMO阵列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数组中的用户名". 如果它们确实匹配,我想用用户名,站号,aht_value创建一个数组,如下所示:

I want to check every "username" in my MEMO array to match the "username" in my USER array. If they do match, I want to create an array with username, station, aht_value like so:

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

//creating array 3 by comparing 1 and 2 by using key value of "username" from array 2
    $result = array();
    //$m = key
    //$m['username'] = key value
    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;

错误消息

Warning: Cannot use a scalar value as an array in line 97 : 
foreach($memo as $m => $m['username']){

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

If I need to clarify things please ask. After successfully creating the third array I will use json_encode to make an AJAX call and use type GET.

提前感谢您的帮助!

推荐答案

我在理解数组和键值之后找到了解决方案.如果有人想做类似的事情,这是两个可能的答案.

I found the solution after understanding arrays and key values.. these are two possible answer if ever someone wants to do something similar.

答案1:

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'];   
      }
    } 
    }

答案2:

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[] = array( 'username'  => $userdata['username'],
                                             'aht_value' => 'NA',
                                             'station'  => $userdata['station']
                                            );
        } 
    else {
        $result[] = array( 'username'  => $userdata['username'],
                                             'aht_value' => substr($memodata['aht_value'],0,-3),
                                             'station'   => $userdata['station']
                                            );
         }
    }
}

这篇关于如果两个数组中都存在值,则使用KEY创建一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆