我想在PHP中使用过滤器数组。
假设我在一个数组中有很多URL。
例如; $someArray
即;
Array (
[0] => javascript:signin('https://login.alibaba.com')
[2] =>http://www.alibaba.com
[3] => http://www.alibaba.com/Products?cd=buyhome
[4] => http://www.alibaba.com/today_new/catalogs/0.html
[5] => http://www.alibaba.com/help/search-for-products.html
[6] => http://resources.alibaba.com/trade_safe/home.htm
[7] => http://us.my.alibaba.com/product/buyoffer/myalibaba/post_buying_lead_no_member.htm [8] => http://www.alibaba.com/sell/sell.htm
[9] => http://sourcing.alibaba.com/rfq_search_list.htm?availability=y&tracelog=sell_br_20111229
[10] => http://www.alibaba.com/help/how_to_sell/join_alibaba.html
[11] => http://us.my.alibaba.com/product/post_product.htm
[12] => http://resources.alibaba.com/
[13] => http://ask.alibaba.com
[14] => http://resources.alibaba.com/forum/trade_related.htm
[15] => http://tradeshow.alibaba.com/
[16] => http://us.my.alibaba.com/
[17] => http://us.my.alibaba.com/mcadmin/inbox/inboxList.htm
[18] => http://us.my.alibaba.com/product/post_product_interface.htm
[20] => http://trademanager.alibaba.com/
[21] => http://www.alibaba.com/trade/servlet/page/static/paid_memberships/index
[22] => http://us.favorite.alibaba.com
[23] => http://www.alibaba.com/trade/help/helpcenter
[24] => http://www.alibaba.com/help
[25] => http://resources.alibaba.com/trade_safe/complaint.html
[26] => http://legal.alibaba.com/legal/site/login/login.htm?site_type=international&language_id=english
[27] => http://www.alibaba.com/help/contact-us.html#askquestion
[28] => javascript:showFeedBackWindow()
[29] => javascript:void(0)
[30] => http://www.example.com/help
[30] => http://www.example.com/about
);
请参阅粗体值。我只想在数组中使用这些值...意味着我要删除除了这些值之外的所有其他值,其值为example.com。
请帮忙。
答案 0 :(得分:1)
我希望这会有所帮助
$someArray = array('http://www.alibaba.com', ' http://us.my.alibaba.com/', 'http://www.example.com/help', 'http://www.example.com/di' );
$goodLink = 'example.com';
foreach ($someArray as $key => $link) {
if(strpos($link, $goodLink) === false) unset($someArray[$key]);
}
输出
Array ( [2] => http://www.example.com/help [3] => http://www.example.com/di )
答案 1 :(得分:0)
使用array_map()
& strpos()
。例如:
$url_array = [
'http://www.google.com/',
'http://www.example.com/help',
'http://www.example.com/about',
'http://www.facebook.com/',
];
$result = array_filter(array_map(function($v){
if(strpos($v, 'example.com') !== false){
return $v;
}
}, $url_array));
print '<pre>';
print_r($result);
print '</pre>';
<强>参考:强>
答案 2 :(得分:0)
将array_filter与回调函数一起使用,如下所示
$someArray = array("javascript:signin('https://login.alibaba.com')", "http://www.example.com/help", "http://www.example.com/about", "http://www.alibaba.com");
$new_arr = array_filter($someArray, "filter");
function filter($element) {
if (strpos($element,'example.com') !== false) {
return $element;
}
else {
return;
}
}
print_r($new_arr);
输出
Array
(
[1] => http://www.example.com/help
[2] => http://www.example.com/about
)