开发者

Parsing only specific values from an array?

I'm parsing some information using Xpath and it returns me a simple array.

$values = array();

Array
(
    [0] => http://www.aaa.com/19364328526/
    [1] => http://www.bbb.com/207341152011/
    [2] => http://www.ccc.co.jp/1246623/
)

Is there any way I can parse through the array and only take certain URLs based on URL weighting? For example. If aaa.com exists, take only aaa.com. If not, check for ccc.co.jp, if that exists, take that only, etc.

I only know how to select from arrays when I know what is there $values[0]/[1]/etc, unfortunately the order of links in this array change and/or aren开发者_开发百科't present sometimes.

Any help would be much appreciated!

Thanks! Tre


You can use in_array() to check if a value exists. I don't know exactly what you are trying to do, but here is an example. Do you know all the possible values that you might get back?

//List domains in priority order    
$weighted = array('aaa.com','bbb.com','ccc.com');

$selected_url = '';
foreach($weighted as $check) { //start with highest priority
    foreach($values as $url) { //loop through all URL's
        if(strpos($url,$check) !== false) {
            //If a url matches priority, return it. We are finished to exit both loops
            $selected_url = $url;
            break 2;
        }
    }
}

$selected_url should have the highest priority URL, or it will be empty if none of the urls were found.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜