I have 2 arrays, each of them var_dump()
'ed below.
I've tried with array_intersect()
, but nothing is returned, and no errors:
if (array_intersect($userInterests, $interests)) {
echo "found";
}
Small array, $userInterests:
array(3) {
[0]=>
object(stdClass)#8 (1) {
["interestId"]=>
string(1) "2"
}
[1]=>
object(stdClass)#6 (1) {
["interestId"]=>
string(1) "3"
}
[2]=>
object(stdClass)#9 (1) {
["interestId"]=>
string(1) "5"
}
}
Large array, $interests:
array(15) {
[0]=>
object(stdClass)#2 (2) {
["interestName"]=>
string(5) "Musik"
["interestID"]=>
string(1) "1"
}
[1]=>
object(stdClass)#11 (2) {
["interestName"]=>
string(3) "Mad"
["interestID"]=>
string(1) "2"
}
[2]=>
object(stdClass)#12 (2) {
["interestName"]=>
string(6) "Rejser"
["interestID"]=>
string(1) "3"
}
[3]=>
object(stdClass)#13 (2) {
["interestName"]=>
string(10) "Mad Moneyz"
["interestID"]=>
string(1) "4"
}
[4]=>
object(stdClass)#14 (2) {
["interestName"]=>
string(5) "Biler"
["interestID"]=>
string(1) "5"
}
[5]=>
object(stdClass)#15 (2) {
["interestName"]=>
string(7) "Netflix"
["interestID"]=>
string(1) "6"
}
[6]=>
object(stdClass)#16 (2) {
["interestName"]=>
string(26) "Lange gåture på stranden"
["interestID"]=>
string(1) "7"
}
[7]=>
object(stdClass)#17 (2) {
["interestName"]=>
string(15) "Bjergbestigning"
["interestID"]=>
string(1) "8"
}
}
My goal is to mark select options as selected, like this:
<option value="<?php echo $interest->interestID; ?>"<?php echo (in_array($interest->interestID, $userInterests)) ? ' selected="selected"' : ''; ?>><?php echo $interest->interestName; ?></option>
$interest is the specific interest taken out from the bigger array, $interests
$userInterests is the smaller array