Clarkeez Posted August 19, 2011 Share Posted August 19, 2011 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 Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 19, 2011 Share Posted August 19, 2011 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. Quote Link to comment Share on other sites More sharing options...
Clarkeez Posted August 19, 2011 Author Share Posted August 19, 2011 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 ) Quote Link to comment Share on other sites More sharing options...
Clarkeez Posted August 19, 2011 Author Share Posted August 19, 2011 It's still returning false. :S Quote Link to comment Share on other sites More sharing options...
Clarkeez Posted August 19, 2011 Author Share Posted August 19, 2011 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? Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 19, 2011 Share Posted August 19, 2011 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))); Quote Link to comment Share on other sites More sharing options...
Clarkeez Posted August 19, 2011 Author Share Posted August 19, 2011 Dear mjdamato You are a star Thanks for you help buddy, you really made it clear for me + I didn't even know array_intersect() exists Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 19, 2011 Share Posted August 19, 2011 + I didn't even know array_intersect() exists 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. Quote Link to comment 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.