Monday, May 14, 2012

search in multidimensional array php

I have a multidimensional array with various sites links, here is output:



Array
(
[0] => Array
(
[0] => http://www.msn.com/etc
[1] => http://www.yahoo.com/etc
[2] => http://www.google.com
)

[1] => Array
(
[0] => http://www.abc.com/etc
[1] => http://www.hotmail.com/etc
[2] => http://www.hotmail.com/page/2
)

[2] => Array
(
[0] => http://www.live.com/etc
[1] => http://www.google.com/etc
[2] => http://www.stock.com
)

)


I wants to match multiple URL's, here my example code:



$sites = array("msn.com","hotmail.com","live.com");
$links = array(
array("http://www.msn.com/1","http://www.yahoo.com/etc","http://www.google.com"),
array("http://www.msn.com/2","http://www.hotmail.com/","http://www.hotmail.com/page/2"),
array("http://www.live.com/etc","http://www.google.com/etc","http://www.stock.com")
);


I need whatever sites are in $sites,first it will find msn.com site from $links array, so if it found msn.com in first array($links[0]) it will not search msn.com in other $links array but keep searching for other (hotmail.com and live.com), and if it find 2 links of same host in one array, it will join them, means if it finds a host in one array element it will not search that host in other elements of $links array, so final output from above will be this:



Array
(
[msn] => Array
(
[0] => http://www.msn.com/1
)

[hotmail] => Array
(
[0] => http://www.hotmail.com/
[1] => http://www.hotmail.com/page/2
)

[live] => Array
(
[0] => http://www.live.com/etc
)

)


I am not sure how to perform this task, I would be grateful for any input. Thanks





No comments:

Post a Comment