0

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!

Evan
  • 1
  • 2

2 Answers2

2

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);
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • Code-only answers are low-value on Stackoverflow because they do a poor job of educating/empowering the OP and thousands of future researchers. Every answer should be explained -- even the simple/basic/self-explanatory/declarative ones. – mickmackusa Apr 07 '20 at 20:53
  • Are you mass posting those comments? Or are you looking for an explanation on this topic in particular? In the first case, please no! – hek2mgl Apr 08 '20 at 07:23
  • Your answer is not so bad because it at least links to documenation. I will stop encouraging people to post complete answers when people start posting complete answers. Code-only answers are too prevalent here and it only encourages new users to snippet drop. Not good for SO. We can argue about this or you can spend the same amount of time to edit your post into something valuable to researchers. If you have plenty of time to burn, you can find a canonical to close this page with. – mickmackusa Apr 08 '20 at 07:34
0

Try this :

$array = array (
   array('href' => 'http://132', 'title' => 'yxz'),
   array('href' => 'http://233', 'title' => 'abc'),
   array('href' => 'http://324', 'title' => '123')
);

$sort = array();
foreach($array as $k=>$v) {
    $sort['title'][$k] = $v['title'];
}

array_multisort($sort['title'], SORT_ASC, $array);


echo "<pre>";
print_r($array);
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
  • Code-only answers are low-value on Stackoverflow because they do a poor job of educating/empowering the OP and thousands of future researchers. Every answer should be explained -- even the simple/basic/self-explanatory/declarative ones. – mickmackusa Apr 07 '20 at 20:53