Php 如何使用in_array()函数创建数组?
我在处理阵列时遇到问题。我想“比较”两个数组,看看两个数组中是否有匹配的“用户名”。我不确定是否正确使用了in_array()函数 这就是我的阵列的外观: 用户阵列1:Php 如何使用in_array()函数创建数组?,php,arrays,multidimensional-array,mysqli,associative-array,Php,Arrays,Multidimensional Array,Mysqli,Associative Array,我在处理阵列时遇到问题。我想“比较”两个数组,看看两个数组中是否有匹配的“用户名”。我不确定是否正确使用了in_array()函数 这就是我的阵列的外观: 用户阵列1: Array ( [0] => Array ( [username] => LNDP [station] => D08 ) [1] => Array ( [username] => ACMAN [station] => D06 ) [2] => Arra
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']
);
}
我想检查备忘录数组中的每个“用户名”,以匹配用户数组中的“用户名”。
如果它们确实匹配,我想创建一个包含用户名、station、aht_值的数组,如下所示:
Array ( [0] => Array ( [username] => ACMAN [aht_value] => 456 [station] => D06 )
)
我尝试的是:
//通过使用数组2中的“username”键值比较1和2来创建数组3
$result=array();
//$m=钥匙
//$m['username']=键值
foreach($m=>m['username']形式的备忘录){
//如果数组2中的用户名在数组1中
if(在数组中($m,$user)){
//如果aht_值不为空
如果($memo['aht_value']!=null){
$result[]=“用户名:”.$user['username']”。“aht_值:”.$m['aht_值']”。“位置:”.$user['position']”.“
”;
}
//否则,如果aht_值为空
否则{
$result[]=“用户名:”.$user['username']”。aht_值:NA位置:“.$user['position']”“
”;
}
}
//如果没有匹配的用户名,请执行某些操作?
否则{
呼应“不匹配”;
}
}
$final_result=json_encode($result);
echo$最终结果;
如果我需要澄清,请询问。成功创建第三个数组后,我将使用json_encode进行AJAX调用,并使用type GET
提前感谢您的帮助 可能有更好的方法,这应该可以实现您想要的:
$data = array()
foreach($memo as $m){
foreach($user as $u){
if($m['username']===$u['username']){
$m['station'] = $u['station'];
$data[] = $m;
}
}
}
但如果我是你,我会设法在用户名或id上使用SQL联接合并记录,以便只执行一个查询。尝试以下操作:
<?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";
}
}
?>
这种方法不是很有效,因为必须循环两个数组。SQL联接可能工作得更好,或者您可以向数组添加唯一索引。(如果用户名重复,这将不起作用,因为关联数组不能有重复的键)
错误非常简单。您需要在foreach的=>中使用像$username
这样的东西,然后在foreach中使用像$username['username']
这样的东西?foreach($username=>$user['username']){这使我对loopNooo中的所有内容都“不匹配”。foreach($key=>$value){echo$value['username'];}
。类似的。代码太多了。只需给出两个数组并告诉您要执行的操作。$m
将是数组中的一个键,它只是一个字符串/int。您不能使用该字符串/int作为数组来分配foreach的值。我以前有一个类似的循环,但我想创建一个for循环。没有memo数组中的“station”如何将其与$u['station']进行比较?谢谢仔细阅读我的代码,正在比较用户名,并且将station附加到memo中我尝试了您的代码,但得到的输出是[]有什么想法吗?thanksI尝试了你的两种方法,也修改了一些东西,但我一直收到一堆结果和错误消息,比如:未定义的索引:第175行中的位置是**$result[]=“username:”.$userdata['username']”。“aht_值:NA position:”.$userdata['position'])。
“;**它也跳过ELSE语句。它只转到“IF”。这意味着在数组中找不到键“position”。它在输出中,但两个示例数组都没有。你能var\u dump
你真正的数组吗?比如,整个结构吗?如果它们很大,试试pastebin。这是,这就是“错误”是一个通知
,这是因为您仍然有位置
作为数组键。将其更改为站
或数组中存在的任何内容。在我的回答中它是位置
,因为它在原始代码中是位置
。
<?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";
}
}
?>
$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);
<?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);