Jump to content

Working with multidimensional array's and sorting them


the5thace

Recommended Posts

The following script no longer returns any results. I have loaded search engine results in to multidimensional arrays. Then I compare the different search engine result sets in order to find url matches.

 

I create one main array for all the scores. I load one results set in to the main array and then compare the url's of it with the second results set. If there is a match then I  add the scores if not then the full result goes to the bottom of the main array. I repeat the process wit the third results set. 

 

I finish it by trying to sort it. 

$i=0;
        foreach ($js->RESULT as $item)
        {
        
                         $Blekko[$i] =  array 
                        ('url'=>$item->url, 
                         'url_title' =>$item->url_title,     
                         'snippet' =>$item->snippet,
                         'rank' => 100-$i);
                     
      }

^ We read in results in to a multidimensional array. Same is applied for the other two search engines that we aggregate. 

 

 

$m=0;
$j=0;
switch ($agg)
{
    case 'Aggregated':
    while ($m<=$b)
    {
        $r_set[$m] = $bing_results[$m]; //load bing result in to main array
        $m++;
    }       
    while ($j<=$b)
    {
        if ($r_set[$j]-->'url' === $Blekko[$j]-->'url') //compare bing url with blekko url for match
        {
            $r_set[$j]['rank'] = $r_set[$j]['rank'] + $Blekko[$j]['rank']; //if match add the ranks together       
        }                                                                    
        else 
        {
            $r_set[$m] = $blekko[$j];
            $m++; // if no match then we send the blekko array to the bottom of main array
        }    
        $j++;
    }
    $reset = 0;
    while ($reset <= $i+$b)
    {
        if ($r_set[$reset]-->'url' === $Faroo[$reset]-->'url') //same procedure
        {
            $r_set[$reset]['rank'] = $r_set[$reset]['rank'] + $Faroo[$reset]['rank'];
        }
        else 
        {
            $r_set[$m] = $Faroo[$reset];
            $m++;
        }    
        $reset++;
    }
    
    array_multisort($r_set, SORT_ASC, 'rank'); //sort the array by rank score
    $echo_l = 0;
    while ($echo_l<=$m)
    {
        echo $r_set[$echo_l];
        $echo_l++;
    }

 

 

<?php

$r_set[0] = array
    ('url' => "https://www.example.com/",
    'url_title' => "Example",
    'snippet' => "Example",
    'rank' => 5);
$r_set[1] = array
    ('url' => "http://forums.phpfreaks.com/",
    'url_title' => "PHPFreaks",
    'snippet' => "PHP Help",
    'rank' => 23);
$r_set[2] = array
    ('url' => "http://us1.php.net/manual/en/function.uasort.php",
    'url_title' => "PHP Manual",
    'snippet' => "PHP Help",
    'rank' => 1);

// Print the array as is:
echo '<h2>Array As Is</h2><pre>' . print_r($r_set, 1) . '</pre>';

//Rank sorting function:
function sort_by_rank($x, $y) {
    return ($x['rank'] > $y['rank']);
}

// Sort by Rank:    
uasort($r_set, 'sort_by_rank');
echo '<h2> Array Sorted by Rank</h2><pre>' . print_r($r_set, 1) . '</pre>';


Maybe something like the above?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.