Jump to content

Searching against an array?


gurpalrattu

Recommended Posts

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 by gurpalrattu
Link to comment
Share on other sites

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.

Link to comment
Share on other sites


$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>";
}
}

}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.