简体   繁体   中英

How do I check if an array contains values from another array?

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

Merge them with array_merge() and compare the number of elements sizeof()

$array1 = array(1,2,3,4,5);
$array2 = array(6,7,8,9,10);

if(sizeof(array_merge($array1, $array2)) < sizeof($array1) + sizeof($array2))
    //Common values

To check if an element is in the array use in_array()

The issue is that your first array is actual an array of objects. And you are basically asking your PHP does this array of objects contain 3 ? This obviously is not a valid question. You need to convert the array of objects to a group of numbers.

From

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

To

array(3) {
  [0]=>
    string(1) "2"
  [1]=>
    string(1) "3"
  [2]=>
    string(1) "5"
}

First of all, convert your user's interests to a simple array.

$userInterestsSimple = array();
foreach ($userInterests as $userInterest)
    $userInterestsSimple[] = $userInterest->interestId;

Afterwards, your check with in_array() will work properly:

<option value="<?php echo $interest->interestID; ?>"
  <?php
    echo (in_array($interest->interestID, $userInterestsSimple)) ? ' selected="selected"' : '';
  ?>
>
    <?php echo $interest->interestName; ?>
</option>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM