Jump to content

Help with searching in an array


Col

Recommended Posts

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

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';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.