karimali831 Posted February 2, 2011 Share Posted February 2, 2011 Hi folks! I have two arrays: 1) print_r($users1); Array ( [0] => 1 [1] => 2609 [2] => 2612 [3] => 2620 [4] => 2621 [5] => 2624 [6] => 2627 [7] => 2629 ) 2) print_r($users2); Array ( [0] => 1 [1] => 2609 [2] => 2610 [3] => 2611 [4] => 2612 [5] => 2617 [6] => 2618 [7] => 2619 [8] => 2620 [9] => 2621 [10] => 2624 [11] => 2625 [12] => 2626 [13] => 2627 [14] => 2628 [15] => 2629 ) now why does nothing return when I use in_array? if(in_array($users1,$users2)) return true; all values in $users1 is in $users2... Please help, need help fast! Thank you Quote Link to comment https://forums.phpfreaks.com/topic/226475-in_array-not-working/ Share on other sites More sharing options...
kenrbnsn Posted February 2, 2011 Share Posted February 2, 2011 Because that's not what the function in_array does. You can write a small function that does what you want: <?php function my_in_array($ary1,$ary2) { $ret = true; foreach($ary1 as $v) { if (!in_array($v,$ary2)) { return false; } } return $ret; } $users1 = array(1,2609,2612,2620,2621,2624,2627,2629); $users2 = array_merge(array(1),range(2609,2629)); echo '<pre>' . print_r($users1,true) . '</pre>'; //debug echo '<pre>' . print_r($users2,true) . '</pre>'; //debug if (my_in_array($users1,$users2)) { echo "yes"; } else { echo "no"; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/226475-in_array-not-working/#findComment-1168987 Share on other sites More sharing options...
BlueSkyIS Posted February 2, 2011 Share Posted February 2, 2011 or this might work $u1_count = count($users1); $diffs = array_diff($users1,$users2); if (count($diffs) != $u1_count) { // There is at least one match... } else { // There are no matches (all in $users1 are different from $users2) } Quote Link to comment https://forums.phpfreaks.com/topic/226475-in_array-not-working/#findComment-1168990 Share on other sites More sharing options...
karimali831 Posted February 2, 2011 Author Share Posted February 2, 2011 Thanks for your help, I will try this but before I do... only this bit I am not sure what to do with: $users1 = array(1,2609,2612,2620,2621,2624,2627,2629); $users2 = array_merge(array(1),range(2609,2629)); I get my arrays from query: $qual = safe_query("SELECT * FROM ".PREFIX."cup_clans WHERE groupID='$ID' && $type='0' && qual='1' && pm='0'"); while($iq = mysql_fetch_array($qual)) { $qualified_ID[]=$iq['clanID']; } $participants = safe_query("SELECT * FROM ".PREFIX."cup_clans WHERE groupID='$ID' && $type='0'"); while($all = mysql_fetch_array($participants)) { $user[]=$all['clanID']; } if(my_in_array($user,$qualified_ID)) return true; does not return true however or should it? Quote Link to comment https://forums.phpfreaks.com/topic/226475-in_array-not-working/#findComment-1169000 Share on other sites More sharing options...
kenrbnsn Posted February 2, 2011 Share Posted February 2, 2011 The $users1 & $users2 in my code was just a way of populating the arrays like you had them in your code. Try reversing the order of the input parameters to my function. The first parameter is the array that contains the smaller number of elements. What are you trying to accomplish. There may be an easier way to do it using one MySQL query. Ken Quote Link to comment https://forums.phpfreaks.com/topic/226475-in_array-not-working/#findComment-1169004 Share on other sites More sharing options...
karimali831 Posted February 2, 2011 Author Share Posted February 2, 2011 Yes I understand that but this.. $users2 = array_merge(array(1),range(2609,2629)); you are merging and using range? why I must do this? I don't know what to put in range. Quote Link to comment https://forums.phpfreaks.com/topic/226475-in_array-not-working/#findComment-1169011 Share on other sites More sharing options...
kenrbnsn Posted February 2, 2011 Share Posted February 2, 2011 You don't have to use $user2, that was just there so I had two arrays that looked like yours. A print_r of $user2 would show: Array ( [0] => 1 [1] => 2609 [2] => 2610 [3] => 2611 [4] => 2612 [5] => 2613 [6] => 2614 [7] => 2615 [8] => 2616 [9] => 2617 [10] => 2618 [11] => 2619 [12] => 2620 [13] => 2621 [14] => 2622 [15] => 2623 [16] => 2624 [17] => 2625 [18] => 2626 [19] => 2627 [20] => 2628 [21] => 2629 ) Ken Quote Link to comment https://forums.phpfreaks.com/topic/226475-in_array-not-working/#findComment-1169020 Share on other sites More sharing options...
karimali831 Posted February 2, 2011 Author Share Posted February 2, 2011 Ahh thanks very much, figured it with the code you mentioned foreach($qualified_ID as $v) { if (in_array($v,$user)) { sendmessage($v, $sm_elig_subject, $sm_qualified); } Quote Link to comment https://forums.phpfreaks.com/topic/226475-in_array-not-working/#findComment-1169023 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.