PHP比较2个数组数组

时间:2014-04-09 17:34:51

标签: php arrays comparison

大家好我在PHP中有两个数组,如下所示:

[users] => Array ( [0] => Array ( [username] => Timothy ) [1] => Array ( [username] => Frederic ) ) 

[users2] => Array ( [0] => Array ( [username] => Johnathon ) [1] => Array ( [username] => Frederic ) [] => Array ( [username] => Peter))

我试图将每个数组的内容相互比较以便放置一个html元素,我尝试使用嵌套的foreach,如下所示:

foreach($users as $user){
    foreach ($users2 as $user2){
        if($user['username'] == $user2['username']){
            echo "<option value=' ".$user['username']."' selected = 'selected'>".$user['username']."</option>";
            break;
        } else{
            echo "<option value=' ".$user['username']."'>".$user['username']."</option>";
        }
    }
}

我的问题是这些项目被多次回显,这会破坏我的选择元素。关于如何比较每个内容的任何想法?

我想要获得每个名字的列表,例如:

-Timothy
-Frederic (this should be highlighted as it is in both arrays)
-Johnathon
- Peter

2 个答案:

答案 0 :(得分:1)

如果您在每个数组中都没有任何重复项,则以下内容适用于我。这可能看起来有点复杂,但请仔细阅读我的评论。

您可以复制代码并在其自己的页面中运行代码,看它是否按照您想要的方式运行。

<?php
//Create user array one
$users = array();
$users[] = array('username'=>'Timothy');
$users[] = array('username'=>'Frederic');

//create user array 2
$users2 = array();
$users2[] = array('username'=>'Johnathon');
$users2[] = array('username'=>'Frederic');
$users2[] = array('username'=>'Peter');

//create a new array to combine all of the data, yes, there will be duplicates
$allData = array();

//add to allData array
foreach ($users as $user) {
    $allData[] = $user['username'];
}

//add to allData array
foreach ($users2 as $user2) {
    $allData[] = $user2['username'];
}

//create an array that will hold all of the duplicates
$dups = array();

//add any duplicates to the array
foreach(array_count_values($allData) as $val => $c) {
    if($c > 1) $dups[] = $val;
}

//remove the duplicates from the allData array
$allData = array_unique($allData);

//echo out form
echo '<select>';
foreach ($allData as $user) {
    if (in_array($user, $dups)) {
        echo "<option value=' ".$user."' selected = 'selected'>".$user."</option>";
    }
    else {
        echo "<option value=' ".$user."'>".$user."</option>";
    }
}
echo '</select>';
?>

但是,我不确定你的意图是什么,因为如果你有&#34; Peter&#34;和&#34;弗雷德里克&#34;在BOTH数组中,你不会得到你想要的形式。但是,这适用于你想要的。

答案 1 :(得分:1)

我会以不同的方式接受它。

//Create user array one
$users = array();
$users[] = array('username'=>'Timothy');
$users[] = array('username'=>'Frederic');

//create user array 2
$users2 = array();
$users2[] = array('username'=>'Johnathon');
$users2[] = array('username'=>'Frederic');
$users2[] = array('username'=>'Peter');

$temp_array = array();

foreach($users as $key => $value) {
    $temp_array[$value['username']] = '';
}

foreach($users2 as $key => $value) {
    $temp_array[$value['username']] = array_key_exists($value['username'], $temp_array) ? 'DUPLICATE' : null;
}

echo '<select>';
foreach($temp_array as $key_value => $status) {
    echo "<option value='{$key_value}' ".(($status == 'DUPLICATE') ? 'selected style="background-color: yellow;"' : '').">{$key_value}</option>";
}
echo '</select>';

我会让数组自己处理它,如果它共享相同的键,它将合并,然后只用“重复”标记它。