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. Link to comment https://forums.phpfreaks.com/topic/77058-help-with-searching-in-an-array/ 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'; ?> Link to comment https://forums.phpfreaks.com/topic/77058-help-with-searching-in-an-array/#findComment-390260 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! Link to comment https://forums.phpfreaks.com/topic/77058-help-with-searching-in-an-array/#findComment-390271 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.