Jump to content

in_array not working?


karimali831

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/226475-in_array-not-working/
Share on other sites

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

 

 

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?

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

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.