php - How to use the array intersect method to match two values within 2 sets of arrays -
i have query:
$subcommon= mymodel::selectraw('subjectlist_id, count(subjectlist_id) aggregate') ->wherein('user_id', [auth::id(), $user->id]) ->groupby('subjectlist_id') ->having('aggregate','>',1) ->get();
which gives query result:
{"subjectlist_id":1,"aggregate":2} {"subjectlist_id":3,"aggregate":2}
i have query:
$values = mysecondmodel::where('user_id', auth::id()) ->first();
which gives result:
{"id":1,"subjectlist_id":1, "article_id":4}
because im finding way difficult join these 2 models i've decided output them arrays @ least 1 matching value subjectlist_id
condition execute query i've displayed @ bottom. i'm assuming it's arrayintersect
method im unsure how use it.
$values = mysecondmodel::where('user_id', auth::id()) ->first();
i have far:
$subcommonarray = (array) $subcommon; $valuesarray = (array) $values; $a = $subcommonarray; $b = $valuesarray; $c = array_intersect_key($a, $b); if (count($c) > 0) { $newquery = mysecondmodel::where(user_id, auth::id()) ->first(); }
but think comparing keys , not values. how match values , and key together?
well if $values
, $subcommon
both valid arrays, compare them using array_intersect
function that:
$subjectlist_id = array_intersect($values, $subcommon);
this go , search similarities in values, , construct array in $subjectlist_id
variable of matching values between both arrays.
a example php documentation:
$array1 = array(2, 4, 6, 8, 10, 12); $array2 = array(1, 2, 3, 4, 5, 6); $array3 = array_intersect($array1, $array2); var_dump($array3);
this have such array:
array(3) { [0]=> int(2) [1]=> int(4) [2]=> int(6) }
Comments
Post a Comment