in_array()可以与二维数组的所有第二级数组的第一个值进行比较吗? [英] Can in_array() compare to the first value of all second-level arrays of a two-dimensional array?

查看:74
本文介绍了in_array()可以与二维数组的所有第二级数组的第一个值进行比较吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试针对这种特殊情况找到更简单的解决方案:

I am trying to find a simpler solution for this particular situation:

  1. 我有一个不含键的一维数组( $ array1 ),包含约400个值(字符串)
  2. 然后我要从数据库中获取数据(使用准备好的语句),每行4个值,我用它们来创建二维数组( $ array2a ),稍后再用.
  3. 现在,我想将 $ array1 的值与 $ array2a 的第一行中的值进行比较:我想创建一个所有值的列表> $ array1 ,不是数据库第一行(字段用户名")中的 .
  1. I have a one-dimensional array ($array1) without keys, containing about 400 values (strings)
  2. Then I am fetching data from a database (using a prepared statement), 4 values per row, which I use to create a two-dimensional array ($array2a) which I need later.
  3. Now I want to compare the values of $array1 to the values in the first row of $array2a: I want to create a list of all values of $array1 which are not in the first row of the database (field "username").

我找到了一个可行的解决方案,但似乎有点太复杂了-它在步骤2中使用了两个不同的数组,一个仅包含要与array1比较的字符串,另一个包含从数组中获取的所有4个值我以后需要的数据库:

I found a working solution, but it seems to be a bit too complicated - it uses two different arrays in step #2, one containing only the strings to be compared to array1, the other one containing all 4 values fetched from the database which I need later:

/* $array1 exists already */

$array2a = array();
$array2b = array();

/*  "$db" already contains the code for the database connection */
if($ps = $db->prepare("SELECT username, value2, value3, value4 FROM userlist")) {
    $ps->execute();
    $ps->bind_result($username, $value2, $value3, $value4);
    while($ps->fetch()) {
        $array2a[] = array($username, $value2, $value3, $value4);
        $array2b[] = $username;
    }
    $ps->free_result();
}
$db->close;

/* comparison of $array1 to $array2b: */
foreach($array1 as $x) {
    if(!in_array($x, $array2b)) {
        echo $x."<br>";
    }
}

这给我列出了数据库的 username 列中 not 的所有 $ array1 值的列表.但是,在第二部分( foreach 循环)中,我想使用 $ array2a 而不是 $ array2b ,但是我没有知道如何将 in_array()函数仅应用于 $ array2a 的第二级数组的每个第一值.我尝试使用 if(!in_array($ x,$ array2a [0])){...} ,但这当然不起作用...

This gives me a list of all values of $array1 which are not in the username column of the database. However, in the second part (the foreach loop) I'd like to use $array2a instead of $array2b, but I don't know how to apply the in_array() function to only each first value of the second-level arrays of $array2a. I tried to use if(!in_array($x, $array2a[0])) { ... }, but that doesn't work of course...

推荐答案

您可以使用 array_column()快速获取数据库返回的所有用户名的列表.然后,按照@FelippeDuarte的建议,您可以使用 array_diff()快速从 $ array1 中获取不在数据库结果中的所有用户名:

You can use array_column() to quickly get a list of all usernames returned by your database. Then, as @FelippeDuarte suggested, you could use array_diff() to quickly get all usernames from $array1 that are not in your database result:

// example data set
$array1 = ['bob', 'john', 'mary', 'elizabeth'];
$array2a = [
    ['bob', 2, 3, 4],
    ['mary', 2, 3, 4]
];

$usernamesFromDatabase = array_column($array2a, 0);  // ['bob', 'mary']
$usernamesNotInDatabase = array_diff($array1, $usernamesFromDatabase);

print_r($usernamesNotInDatabase);
/*
Array
(
    [1] => john
    [3] => elizabeth
)
*/

请注意,使用 array_diff()保留了 $ array1 中的数组索引.如果有问题,可以使用 array_values():

Note that the array indices from $array1 are kept using array_diff(). If that's a problem, you can reset the indices using array_values():

$resetIndices = array_values($usernamesNotInDatabase);
print_r($resetIndices);
/*
Array
(
    [0] => john
    [1] => elizabeth
)
*/

这篇关于in_array()可以与二维数组的所有第二级数组的第一个值进行比较吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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