Jump to content

multidimension array + in_array


gh

Recommended Posts

Hello,

 

I've been curious why the following won't work. Could anyone show me what I am doing wrong. The code is to find the TLD in a URL.

 

$a[] = parse_url( 'www.example.com' );
$tld_arr = array ( 'com', 'net', etc...);


for ($i=0; $i<count($this->a); $i++)
{
      if (in_array ( $this->a[$i]['host'], $tld_arr) ) < This is what I'm curious about 
     {
            //do something
     }
// do something else
}

 

Thank you

Link to comment
Share on other sites

The 'host' part would be something like 'example.com' or 'google.com' so you'll have to extract the tld from there first. Just an example:

$a[] = parse_url('http://www.example.com');
$a[] = parse_url('http://google.com');

$tld_arr = array ( 'com', 'net', 'etc' );

foreach ($this->a as $url)
{
	$parts = explode('.', $url['host']);
	if (in_array ( $parts[count($parts)-1], $tld_arr) )
	{
		//do something
	}
// do something else
}

echo "</table>\n";

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.