blacksmoke26 Posted November 17, 2009 Share Posted November 17, 2009 <?php $wgt_places = array('all', 'index', 'view', 'search'); $place = 'all, index'; $data = str_replace(', ', ',', $place); $places = explode (',', $data); if (!in_array ($places , $wgt_places)) { echo 'ERROR'; exit; } echo 'PASS'; exit; ?> Not found error in_array() :'( Anyone can solve this? Quote Link to comment https://forums.phpfreaks.com/topic/181883-solved-in_array-problem/ Share on other sites More sharing options...
Mchl Posted November 17, 2009 Share Posted November 17, 2009 First argument of in_array cannot be an array itself AFAIK. Quote Link to comment https://forums.phpfreaks.com/topic/181883-solved-in_array-problem/#findComment-959246 Share on other sites More sharing options...
MadTechie Posted November 17, 2009 Share Posted November 17, 2009 $places would be an array, your need to use a loop, ie foreach($places AS $p){ if (!in_array ($places , $wgt_places)) { echo 'ERROR'; exit; } } Quote Link to comment https://forums.phpfreaks.com/topic/181883-solved-in_array-problem/#findComment-959247 Share on other sites More sharing options...
premiso Posted November 17, 2009 Share Posted November 17, 2009 First argument of in_array cannot be an array itself AFAIK. It can be as of 4.2 they added that ability, but the how the OP was using it is incorrect. The original array would need to be multi-dimensional and the array needle would need to match an array inside the multi-dimm array being searched exactly, as far as I know But yea, see Mad's response for the solution. EDIT: An alternative to doing the in_array would possibly doing an array_search, depending on what the OP is looking to get out of this. Quote Link to comment https://forums.phpfreaks.com/topic/181883-solved-in_array-problem/#findComment-959256 Share on other sites More sharing options...
blacksmoke26 Posted November 17, 2009 Author Share Posted November 17, 2009 but i found an example in PHP manual: <?php $a = array(array('p', 'h'), array('p', 'r'), 'o'); if (in_array(array('p', 'h'), $a)) { echo "'ph' was found\n"; } if (in_array(array('f', 'i'), $a)) { echo "'fi' was found\n"; } if (in_array('o', $a)) { echo "'o' was found\n"; } ?> In this example, $needle is as array. Me also searching for array to array. Quote Link to comment https://forums.phpfreaks.com/topic/181883-solved-in_array-problem/#findComment-959258 Share on other sites More sharing options...
Alex Posted November 17, 2009 Share Posted November 17, 2009 Your problem is that you're searching for the array $wgt_places, not each element separately. So it would work if your array looked like this: $wtg_places = array(array('all', 'index')); To get the functionality that you want you can do this: foreach($places as $place) { if(!in_array($place, $wdt_places)) { echo "$place was not found<br />\n"; } } Or if you want a single function just to check if all the elements of $places are in $wgt_places something like this will work: function ein_array($search, $array) { $found = true; foreach($search as $element) if(!in_array($element, $array) $found = false; return $found; } Quote Link to comment https://forums.phpfreaks.com/topic/181883-solved-in_array-problem/#findComment-959259 Share on other sites More sharing options...
cags Posted November 17, 2009 Share Posted November 17, 2009 I'm going to assume English isn't your main language. What exactly is your question? Firstly why replace ', ', with ',' then explode on ','. Surely you may as well have just exploded on ', ' in the first place. Secondly, when passing an array as the first argument (the needle), PHP will search the second argument (the haystack) for that array. It will not seach 'the haystack' to see if it contains all values within 'the needle'. As an example, this is a value that will pass your current criteria... $wgt_places = array(array('all', 'index'),'all', 'index', 'view', 'search'); $place = 'all, index'; $data = str_replace(', ', ',', $place); $places = explode (',', $data); print_r($places); if (!in_array ($places , $wgt_places)) { echo 'ERROR'; exit; } echo 'PASS'; exit; It seems more like you want to do something like this... foreach($places as $v) { if(!in_array($v, $wgt_places)) { echo 'Error'; exit; } } echo 'Pass'; exit; Edit: D'oh, snap! Quote Link to comment https://forums.phpfreaks.com/topic/181883-solved-in_array-problem/#findComment-959263 Share on other sites More sharing options...
blacksmoke26 Posted November 17, 2009 Author Share Posted November 17, 2009 Thanks! AlexWD Quote Link to comment https://forums.phpfreaks.com/topic/181883-solved-in_array-problem/#findComment-959270 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.