【问题标题】:How to create an array with in_array() function?如何使用 in_array() 函数创建数组?
【发布时间】:2014-11-12 14:30:22
【问题描述】:

我在处理数组时遇到问题。我想“比较”两个数组,看看两个数组中是否有匹配的“用户名”。我不确定我是否正确使用了 in_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']                                                          
                            );
    }

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 的数组,如下所示:

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

我尝试了什么:

//通过使用数组2中“用户名”的键值比较1和2来创建数组3 $结果 = 数组(); //$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 类型。

提前感谢您的帮助!

【问题讨论】:

  • 错误非常简单。您需要在 foreach 的 => 中使用 $username 之类的东西,然后在 foreach 中使用 $username['username'] 之类的东西。
  • 所以这个? foreach($memo as $username => $user['username']){ 这让我对循环中的所有内容都“不匹配”
  • 不。 foreach($memo as $key =&gt; $value) { echo $value['username']; }。类似的东西。
  • 代码太多。只需给出两个数组并告诉您想要做什么。
  • $m 将成为您数组中的 KEY,它只是一个字符串/整数。您不能使用该字符串/int 作为数组来分配 foreach 的 VALUE。

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


【解决方案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 可能会更好,或者您可以向数组添加唯一索引。 (如果用户名重复,这将不起作用,因为关联数组不能有重复的键)

<?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);

【讨论】:

  • 我尝试了两种方法并修改了一些东西,但我不断收到一堆结果和错误消息,例如:Undefined index: position in line 175 being ** $结果[] =“用户名:”。 $userdata['username'] 。 “aht_value:NA 位置:”。 $userdata['位置'] 。 "
    ";** 它也跳过了 ELSE 语句。它只适用于“IF”。
  • 这意味着在数组中找不到键“位置”。它在您的输出中,但您的示例数组都没有它。
  • 你能var_dump你的真实数组吗?比如,整个结构?如果它们很大,请尝试使用 pastebin。
  • 这是user array,这是memo array
  • “错误”是Notice,是因为您仍然有position 作为数组键。将其更改为 station 或阵列中存在的任何内容。在我的回答中是position,因为在您的原始代码中它是position
【解决方案2】:

可能有更好的方法,这应该可以实现您想要的:

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

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

【讨论】:

  • 我有一个与之前类似的循环,但我想制作一个 for 循环。 memo 数组中没有“station”,那么如何将其与 $u['station'] 进行比较?谢谢
  • 仔细阅读我的代码,正在比较用户名,并将站附加到备忘录中
【解决方案3】:

试试这样的:

<?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";
        }

    }          
?>

【讨论】:

  • 我试过你的代码,但我得到的输出是 [] 有什么想法吗?谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-22
  • 2017-12-27
  • 2021-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-19
相关资源
最近更新 更多