Hatdrawn Posted June 11, 2009 Share Posted June 11, 2009 How would I build a function that would test each key of one associative array to all the values of another arry? This is what I have, quite clearly not it but hey, I'm trying foreach($_POST as $var => $value) { $n=0; if($var == $arr_auto[$n]){ echo $var . ' : ' . $value . "<br>"; } $n++; } Link to comment https://forums.phpfreaks.com/topic/161756-solved-compare-one-value-of-array-against-all-the-values-of-another/ Share on other sites More sharing options...
Hatdrawn Posted June 11, 2009 Author Share Posted June 11, 2009 foreach($_POST as $var => $value) { $n=0; if(in_array($var, $arr_auto)){ echo $var . ' : ' . $value . "<br>"; } $n++; } This is what I wanted, thought I'd post for someone elses reference Link to comment https://forums.phpfreaks.com/topic/161756-solved-compare-one-value-of-array-against-all-the-values-of-another/#findComment-853469 Share on other sites More sharing options...
Cardale Posted June 11, 2009 Share Posted June 11, 2009 This gives a good basic understanding..now put it into a class and use a template system and see if you can get away with that. Link to comment https://forums.phpfreaks.com/topic/161756-solved-compare-one-value-of-array-against-all-the-values-of-another/#findComment-853475 Share on other sites More sharing options...
Hatdrawn Posted June 11, 2009 Author Share Posted June 11, 2009 This gives a good basic understanding..now put it into a class and use a template system and see if you can get away with that. So what is wrong with this technique? It's the only solution I could find. Link to comment https://forums.phpfreaks.com/topic/161756-solved-compare-one-value-of-array-against-all-the-values-of-another/#findComment-853484 Share on other sites More sharing options...
.josh Posted June 11, 2009 Share Posted June 11, 2009 $array1 = array ('a' => 1,'b' => 1,'c' => 1); $array2 = array ('a','b','d'); $newarray = array_intersect_key($array1,array_flip($array2)); echo "<pre>"; print_r($newarray); output: Array ( [a] => 1 [b] => 1 ) Link to comment https://forums.phpfreaks.com/topic/161756-solved-compare-one-value-of-array-against-all-the-values-of-another/#findComment-853531 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.