Change your code to the following:
$cat_id = array( 0 => 76 , 1 => 89 , 2 => 81 );
for( $x = 0 , $c = count( $query_cat_id ) ; $x < $c ; $x++ ){
if( in_array( $query_cat_id[$x] , (array) $cat_id ) ){
// MATCH FOUND
}
}
1) If the code provided in your Question was as-is, the way you declared your array was unusual (and possibly faulty),
2) Including (array)
before $cat_id
should ensure that it is handled as an array, even if it is actually a string (it gets instantly converted into an array for the purpose of that function).
I realise that these suggestions may not be accurate, as they are based off the data you copied and pasted into SO, as opposed to the actual data in the scripts, but they may give you a place to start.
Possible Alternate Solution
If the purpose of the code is to compare two arrays, and perform some action when values are found to be within both, the following may be a faster way to perform this work:
$cat_id = array( 0 => 76 , 1 => 89 , 2 => 81 );
//$query_cat_id is assumed as set to an array already
$matched_cat_ids = array_intersect( (array) $query_cat_id , (array) $cat_id );
foreach( $matched_cat_ids as $k => $v ){
// Perform work required on MATCH for Value "$v"
}
That is assuming you need specific work performed for each match. If you simply want to know whether a match has occurred, checking that count($matched_cat_ids)
is greater than zero should suffice.
var_dump($cat_id);
just before the for loop and show us the results.count()
function as a condition in afor
loop, unless it changes during the loop. It's recalculated every iteration. Set a variable to the result ofcount
and use that.if(in_array($listing_id, $purchases_array[$user_id])){...
second argument WAS an array but it didn't work before I changed to this:if(in_array($listing_id, (array)$purchases_array[$user_id])){...
thanks @Lucanos