gurpalrattu Posted February 22, 2015 Share Posted February 22, 2015 (edited) I'm working on an assignment for my PHP class and I'm stuck. Basically what I'm trying to do is take the input from an html form and check that it matches against a key or a value in an associative array I created in a separate php file. I'm printing each pair like "key => value", and if either the key or value matches then that line will be bolded. I'm using the array_key_exists and in_array functions to match against the array, but whats happening is if it finds that the input matches then every line becomes bolded. How do I make it so that one one selected line will be bolded and the rest will print out normally? I can post the text from my file if needed, but I'd rather do it this way as I don't want to just be told what to write, I wanna learn this. Edit: I'm also using the foreach function in conjunction with the other two functions, so I understand why they're all printing out bolded, I just would like to know what how use to just print one line Edited February 22, 2015 by gurpalrattu Quote Link to comment https://forums.phpfreaks.com/topic/294826-searching-against-an-array/ Share on other sites More sharing options...
Barand Posted February 22, 2015 Share Posted February 22, 2015 pseudocode get input for each array element if matches input print bold else print plain end if end for Quote Link to comment https://forums.phpfreaks.com/topic/294826-searching-against-an-array/#findComment-1506483 Share on other sites More sharing options...
gurpalrattu Posted February 24, 2015 Author Share Posted February 24, 2015 Okay, so I figured out how to check a string against an array and I can get the result that I want, but if I put in more than one string then it stops working. this is the function I'm using: function tester($comp,$search){ ksort($comp); foreach ($comp as $key => $value){ if ($search == $key || $search == $value){ print "<strong>"."*=*=*$key ==> $value"."</strong>"."<br />"; } else { print "$key ==> $value <br />"; } } } As you can see I am passing in the array($comp) and the search values($search), if there is only one search term then this works, but if I put in more than one then it doesn't work. I tried to use the explode function, but that doesn't seem to work and now I'm stuck and I'm not sure what to do. Quote Link to comment https://forums.phpfreaks.com/topic/294826-searching-against-an-array/#findComment-1506564 Share on other sites More sharing options...
Barand Posted February 24, 2015 Share Posted February 24, 2015 $comp = array( 'aaa' => 'bbb', 'ccc' => 'ddd', 'eee' => 'fff', 'bbb' => 'ggg', 'hhh' => 'kkk', 'mmm' => 'nnn', 'ppp' => 'qqq', 'rrr' => 'sss', 'ttt' => 'ccc' ); $search = 'kkk,ccc,bbb'; tester($comp, $search); function tester ($comp, $search) { ksort($comp); $asrch = explode(',', $search); // convert to array foreach ($comp as $k=>$v) { if (in_array($k, $asrch) || in_array($v, $asrch)) { echo "<strong>$k => $v</strong><br>"; } else { echo "$k => $v<br>"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/294826-searching-against-an-array/#findComment-1506579 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.