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

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.