Domanda

Hi All I have 2 arrays for example in PHP as seen below:

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

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

I am trying to compare the contents of each array against each other in order to put a html element, I tried using a nested foreach as seen below:

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

my issue is that the items are being echoed more than once which is ruining my select element. Any ideas on how to compare the contents of each?

I want to achieve a list of each name eg:

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

Soluzione

I would take it in a different way.

//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>';

I'll let the array take care of it self, if it shares the same key, it will merge, then just flag it with "duplicate".

Altri suggerimenti

If there is never any duplicates as you say in each array, the following works for me. This may look a bit complicated, but read through my comments.

You can copy the code and run it in it's own page to see if it works the way you want.

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

However, I'm not sure what your intentions are since IF you had "Peter" and "Frederic" in BOTH arrays, you are not going to get the form you want. But, this works for what you wanted.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top