Col Posted November 12, 2007 Share Posted November 12, 2007 Hello, please forgive my noobness when it comes to PHP, I only started doing it last week. What I need help with is this: I am trying to find the key of an array whose value contains two specific words somewhere in it. The array contains lines of a html page, and I need to find where a certain part is in it. Eg: $name = theword array { [1] => whatever [2] => whatever [3] => <td><a href="info.php?id=7647" >theword</a></td> [4] => whatever } -- How can I search the array to find out which key has info.php and $name in it? There is only 1 instance of it, if it exists on the page that is. Thank you. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 12, 2007 Share Posted November 12, 2007 try <?php $name = 'theword'; $stuff = array ( 1 => 'whatever' , 2 => 'whatever' , 3 => '<td><a href="info.php?id=7647" >theword[/url]</td>' , 4 => 'whatever' ); $found = false; foreach ($stuff as $k=>$line) { if (strpos($line, 'info.php') && strpos($line, $name)) { $found = true; break; } } echo $found ? $k : 'Not found'; ?> Quote Link to comment Share on other sites More sharing options...
emehrkay Posted November 12, 2007 Share Posted November 12, 2007 Cool, I didnt know that you could use the key/val variables outside of the foreach block. Just learned soemthing! Quote Link to comment 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.