Syto Posted August 19, 2007 Share Posted August 19, 2007 Hi all! Very new to php (current running php 4.4) and I am really getting into it! I have been getting on fine with basic coding until I stumbled accross a problem. I cant seem to figure out how to check to see if a value in one array is in another array - and if so just simple report back "true" or "false". Lets call the two arrays LOCKS and KEYS. Doing a print_r($locks) would give: Array ( [0] => 9 [1] => 17 [2] => 26 [3] => 27 [4] => 28 [5] => 31 [6] => 34 [7] => 38 [8] => 40 [9] => 41 [10] => 46 [11] => 49 [12] => 53 [13] => 54 [14] => 56 [15] => 60 [16] => 10 [17] => 11 [18] => 14 [19] => 19 [20] => 20 [21] => 22 [22] => 21 [23] => 23 [24] => 24 [25] => 25 [26] => 29 [27] => 30 [28] => 32 [29] => 33 [30] => 36 [31] => 37 [32] => 39 [33] => 42 [34] => 43 [35] => 44 [36] => 45 [37] => 47 [38] => 48 [39] => 50 [40] => 51 [41] => 52 [42] => 55 [43] => 58 [44] => 8 [45] => 12 [46] => 16 [47] => 18 [48] => 35 [49] => 59 [50] => 57 [51] => 13 [52] => 65 ) and doing a print_r($keys) would give: Array ( [0] => 1 [1] => 2 [2] => 4 [3] => 20 [4] => 65 ) What I want to do is jsut test to see if any value from the $key array is in the $lock array. There maybe be more than one available but I just want it to report back a true or false value... Any help would really be appreciated! thanks! Syto Quote Link to comment https://forums.phpfreaks.com/topic/65708-compare-items-in-an-array/ Share on other sites More sharing options...
hostfreak Posted August 19, 2007 Share Posted August 19, 2007 Try: <?php if (in_array(array_values($keys), $locks)) { //true } else { /false } ?> Edit: hmm don't think this will work. I will try to work up something else, in the meantime someone else might have a solution... Quote Link to comment https://forums.phpfreaks.com/topic/65708-compare-items-in-an-array/#findComment-328183 Share on other sites More sharing options...
Syto Posted August 19, 2007 Author Share Posted August 19, 2007 Thanks but that didnt appear to work. I'm pretty stumped on how to get this working :-\ Quote Link to comment https://forums.phpfreaks.com/topic/65708-compare-items-in-an-array/#findComment-328197 Share on other sites More sharing options...
Syto Posted August 19, 2007 Author Share Posted August 19, 2007 Ok I seem to have found a solution but it seems to be very "dirty". What do you think to this: <?php foreach($locks as $needle){ if (in_array($needle, $keys)) { $valid = true; } } if ($valid != true) { echo "denied"; exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/65708-compare-items-in-an-array/#findComment-328212 Share on other sites More sharing options...
plutomed Posted August 19, 2007 Share Posted August 19, 2007 I've just found out how to do and I have been doing it alot the way I do it: function main_nav() { $main_nav = array( "names" => array("Home", "Pictures", "Templates", "Website Coding", "Search"), "pages" => array("index.php", "pictures.php", "templates.php", "website_coding.php", "search.php"), ); $count_main = count($main_nav['names']); $i_main = 0; while($i_main < $count_main) { $links_main .= " <a href=\"".$main_nav['pages'][$i_main]."\">".$main_nav['names'][$i_main]."</a> "; $i_main++; } echo $links_main; } That is an exaple of one of mine it might help.... Quote Link to comment https://forums.phpfreaks.com/topic/65708-compare-items-in-an-array/#findComment-328213 Share on other sites More sharing options...
Barand Posted August 19, 2007 Share Posted August 19, 2007 http://www.php.net/array_intersect Quote Link to comment https://forums.phpfreaks.com/topic/65708-compare-items-in-an-array/#findComment-328233 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.