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
https://forums.phpfreaks.com/topic/233035-multidimension-array-in_array/
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";

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.