ASC排序foreach [英] ASC sort foreach
本文介绍了ASC排序foreach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这一切都很好,我只想对结果进行排序。
如何在foreach循环中按字母顺序排列记录?
$ b $ pre $函数fetch_google($ terms =示例搜索,$ numpages = 1,$ user_agent ='Mozilla / 5.0(Windows NT 6.1; rv: 8.0)Gecko / 20100101 Firefox / 8.0')
{
$ searching =;
for($ i = 0; $ i <= $ numpages; $ i ++)
{
$ ch = curl_init();
$ url =http://www.google.com/search?hl=zh-TW&q=.urlencode($ terms)。& start =。$ i.'0';
curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch,CURLOPT_USERAGENT,$ user_agent);
curl_setopt($ ch,CURLOPT_HEADER,0);
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch,CURLOPT_REFERER,'http://www.google.com/');
curl_setopt($ ch,CURLOPT_CONNECTTIMEOUT,120);
curl_setopt($ ch,CURLOPT_TIMEOUT,120);
curl_setopt($ ch,CURLOPT_MAXREDIRS,10);
curl_setopt($ ch,CURLOPT_COOKIEFILE,cookie.txt);
curl_setopt($ ch,CURLOPT_COOKIEJAR,cookie.txt);
$搜寻= $ searled.curl_exec($ ch);
curl_close($ ch);
}
$ xml = new DOMDocument();
@ $ xml-> loadHTML($搜寻);
foreach($ xml-> getElementsByTagName('a')as $ lnk)
{
if($ lnk-> getAttribute('class')=='l')$
$ link $ = $($ b $ h $''$'$'$'$'$'$' > nodeValue
);
}
}
return $ links;
}
$ content = fetch_google(exemple,1);
foreach($ content as $ elem)
{
echo< a target = \_blank\href = $ elem [href]> $ elem [标题] LT; / A><峰; br>中;
$ / code $ / pre
我想按行排序$ elem [title] ASC
帮助是非常appriciated!
解决方案
想象这个例子使用函数< hort =http://www.php.net/manual/en/function.usort.php =nofollow> usort()
$ array = array('href'=>'http:// 132',' ('href'=>'http:// 233','title'=>'abc'),
数组('href' =>'http:// 324','title'=>'123')
);
$ b $ usort($ array,function($ a,$ b){
if($ a ['title'] === $ b ['title']){
返回0;
}
返回$ a ['title']< $ b ['title']? - 1:1;
});
var_dump($ array);
The following code get result from google by a function. This all works great I only want sort the results.
How can I sort the records in the foreach loop alphabetical ASC...?
function fetch_google($terms="sample search",$numpages=1,$user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0')
{
$searched="";
for($i=0;$i<=$numpages;$i++)
{
$ch = curl_init();
$url="http://www.google.com/search?hl=en&q=".urlencode($terms)."&start=".$i.'0';
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_REFERER, 'http://www.google.com/');
curl_setopt ($ch,CURLOPT_CONNECTTIMEOUT,120);
curl_setopt ($ch,CURLOPT_TIMEOUT,120);
curl_setopt ($ch,CURLOPT_MAXREDIRS,10);
curl_setopt ($ch,CURLOPT_COOKIEFILE,"cookie.txt");
curl_setopt ($ch,CURLOPT_COOKIEJAR,"cookie.txt");
$searched=$searched.curl_exec ($ch);
curl_close ($ch);
}
$xml = new DOMDocument();
@$xml->loadHTML($searched);
foreach($xml->getElementsByTagName('a') as $lnk)
{
if($lnk->getAttribute('class')=='l')
{
$links[] = array(
'href' => $lnk->getAttribute('href'),
'title' => $lnk->nodeValue
);
}
}
return $links;
}
$content = fetch_google("exemple",1);
foreach($content as $elem)
{
echo "<a target=\"_blank\" href=$elem[href]>$elem[title]</a><br>";
}
I want to sort the rows by $elem[title] ASC
Help is much appriciated!
解决方案
Imagine this example that uses the function usort()
$array = array (
array('href' => 'http://132', 'title' => 'yxz'),
array('href' => 'http://233', 'title' => 'abc'),
array('href' => 'http://324', 'title' => '123')
);
usort($array, function($a, $b) {
if($a['title'] === $b['title']) {
return 0;
}
return $a['title'] < $b['title'] ? - 1 : 1;
});
var_dump($array);
这篇关于ASC排序foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文