Jump to content

Using in_array() in a for loop.


Clarkeez

Recommended Posts

Heyy !

 

I'm running into a problem while trying to use in_array($needle, $haystack[$i]) inside a for loop.

 

I get the following error message..

Warning: in_array() expects parameter 2 to be array, integer given in C:\WebSrv\htdocs\inc\form.class.php on line 40

 

Now here is the code

 

<?php
for($i=0; $i <= $charAchievementsCount; $i++):
if(!in_array($conf['guild']['reqAchievements'], $charAchievements[$i])):
	$charMeetsAchievement = false; else: $charMeetsAchievement = true;
endif;
endfor;
?>

 

Some reason, it kicks that error..

Can't in_array be used within a for loop?

Any suggestions ?

 

Thanks

Link to comment
Share on other sites

Yes, it absolutely can be used in a for loop. But it seems that $charAchievements[$i] is an actual value of the array and not an array itself. If $charAchievements[$i] was a subarray then it would work.

 

The point of in_array() is to check the value against all the values in the array - there is no need to iterate through the array to use in_array() - it does it automatically. The exception is if you need to check a value against a mulch-dimensional array.

 

So, if my assumption is correct that $charAchievements is a single-dimensional array then you don't need a for loop at all. just

	if(!in_array($conf['guild']['reqAchievements'], $charAchievements)):  // <== Removed [$i]
	$charMeetsAchievement = false; else: $charMeetsAchievement = true;
endif;

 

If you are not understanding this or you think I may be misunderstanding this, please provide the output of print_r($charAchievements); so we can see the structure of the array.

Link to comment
Share on other sites

Ok..

Thanks for your reply, It makes much more sense not to need a loop as yes, it is just single dimensional.

Now, I thought dude, you've got it thanks, then in tried it and it still not finding it...

 

My code is now exactly as you have it (which it correct.)

 

Needle variable is a simple array

$conf['guild']['reqAchievements'] = array(399, 402, 406, 5350);

 

Print_r of $charAchievements

Array ( [0] => 6 [1] => 7 [2] => 8 [3] => 9 [4] => 10 [5] => 11 [6] => 12 [7] => 13 [8] => 42 [9] => 43 [10] => 44 [11] => 45 [12] => 46 [13] => 73 [14] => 116 [15] => 154 [16] => 162 [17] => 166 [18] => 168 [19] => 199 [20] => 201 [21] => 208 [22] => 212 [23] => 213 [24] => 214 [25] => 216 [26] => 218 [27] => 223 [28] => 227 [29] => 229 [30] => 231 [31] => 238 [32] => 245 [33] => 247 [34] => 263 [35] => 397 [36] => 398 [37] => 399 [38] => 477 [39] => 478 [40] => 479 [41] => 480 [42] => 481 [43] => 482 [44] => 483 [45] => 484 [46] => 486 [47] => 488 [48] => 500 [49] => 503 [50] => 504 [51] => 505 [52] => 506 [53] => 509 [54] => 512 [55] => 513 [56] => 515 [57] => 516 [58] => 522 [59] => 545 [60] => 556 [61] => 557 [62] => 558 [63] => 584 [64] => 587 [65] => 621 [66] => 627 [67] => 628 [68] => 629 [69] => 631 [70] => 632 [71] => 633 [72] => 634 [73] => 635 [74] => 637 [75] => 639 [76] => 642 [77] => 647 [78] => 648 [79] => 649 [80] => 650 [81] => 653 [82] => 657 [83] => 659 [84] => 666 [85] => 686 [86] => 696 [87] => 697 [88] => 699 [89] => 700 [90] => 706 [91] => 727 [92] => 728 [93] => 731 [94] => 732 [95] => 733 [96] => 734 [97] => 735 [98] => 736 [99] => 750 )

Link to comment
Share on other sites

Ok I think I may know why it's returning false..

Now my needle is array(399, 402, 406, 5350);

 

But, I basically want in_array to return TRUE is it find ANY of these in the needle array..

So I am thinking does in_array try and match ALL in the array to the haystack?

Link to comment
Share on other sites

Ok I think I may know why it's returning false..

Now my needle is array(399, 402, 406, 5350);

 

But, I basically want in_array to return TRUE is it find ANY of these in the needle array..

So I am thinking does in_array try and match ALL in the array to the haystack?

 

You can use an array as the needle. But, if you do it is checking if that array exists as a subarray of the haystack - not the individual values of the needle array. You could loop through each element in the needle and check it against the haystack using in_array(), but there is a simpler solution: array_intersect().

 

array_intersect() will find all instances of the values from the first array that exist in any of the other arrays and returns the result as an array. In this case it would return an array of all the values from $conf['guild']['reqAchievements'] that exist in $charAchievements. If that resulting array has 0 elements then none of the values from $conf['guild']['reqAchievements'] exist in $charAchievements.

if(!count(array_intersect($conf['guild']['reqAchievements'], $charAchievements)))
{
    $charMeetsAchievement = false;
}
else
{
    $charMeetsAchievement = true;
}

 

However, since you are simply setting a value to true/false based upon whether the condition is true/false; you can simplify this by setting the value TO the condition. Just change the condition to return true if results were found.

$charMeetsAchievement = (count(array_intersect($conf['guild']['reqAchievements'], $charAchievements)));

Link to comment
Share on other sites

+ I didn't even know array_intersect() exists :D

 

Which is why there is a manual! If you go to the manual page for in_array() the left hand side of the page lists all the functions related to arrays. Also, each manual page lists similar/related functions under the "See Also" section right after the examples. Whenever you are having an issue with a function not doing what you expect or it doesn't "quote" do what you want always check the "See Also" section. In this case, array_intersect() isn't listed in the See Also section for in_array(), but it is also a good idea to browse through the other functions available. I have stumbled upon many useful functions by just browsing the manual.

 

array_intersect() has five different variations and there are also 5 variations of array_diff(). You should take a look.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.