Forum Moderators: coopster

Message Too Old, No Replies

sort/rank array and remove duplicates

most occurences in array first.

         

ikbenhet1

10:09 pm on May 26, 2003 (gmt 0)

10+ Year Member




$a("one","two","three","three","two","two");

how do i sort this array that it returns

$a("two","three","one");

Any help very much appriciated.

jatar_k

10:28 pm on May 26, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



array_unique [php.net]

sort [php.net]

those should do the trick. Take a look at the other various sorting functions referenced on the sort page as well.

ikbenhet1

10:40 pm on May 26, 2003 (gmt 0)

10+ Year Member



Thanks, for your quick reply. I already know both these functions.
array unique= remove duplicates
sort=sort array

I want to rank the values with the most occurances in the array on top in a new array and after that remove duplicates.

aspr1n

1:20 am on May 27, 2003 (gmt 0)

10+ Year Member



Hi ikbenhet1,

I maybe completely wrong here, and I don't mean to offend you so apologies if I'm saying stuff you know, but I'm struggling to make sense of your code:

$eestgezocht=file("search.txt");

Will create an array of n indicies where n is the number of lines in the file.

$meestgezocht=explode("\n",$eestgezocht);

explode() will attempt to strip the the \n char from the line entry, therefore $meestgezocht won't be an array of strings minus the newline, and anyway will probably fail with an array to string error.

So assuming you've got that far, 'meestgezocht' isn't an array so you can't iterate through it with:

echo $meestgezocht[ $als ];

Without wishing to get personal on your coding style, I'd drop the extensive use of side effects for elegibility, the extra few lines won't slow your code down.

My advice is switch on your error settings because I can't see how the code will work as it stands anyway, and I smell an infinite loop in there somewhere ;)

Also consider substituting:

$gotit=0;
while ( $gotit < 100 ){
$gotit++;
echo $meestgezocht[ $als ];
...

for
for( $gotit=0; $gotit < 100; $gotit++ )

same thing just 10 times easier to read.

In a direct answer to your question:

I don't know of a function to do this automatically, the approach I would take would be to first sort the array, and them iterate through the array pop()ing values off that are '=>' whatever values you want.

Hope this is of some value,

asp

[edit]
ikbenhet1 edited post to remove example code, asp added bits he referred to here for post completeness.
[/edit]

$eestgezocht=file("search.txt");
$meestgezocht=explode("\n",$eestgezocht);
$vlo=0;
$knm=0;
$keyl=0;
$als=1;
$gotit=0;
while ( $gotit < 100 ){
$gotit++;
echo $meestgezocht[ $als ];
$keyl=0;
$knm=1;
while ( $keyl<100 ){
$vlu=0;
if ( trim( strtoupper( $meestgezocht[ $als ] ) ) == trim( strtoupper( $kwd[ $keyl ] ) ) and trim($meestgezocht[$als])!=""){
if ( $counts[$keyl]<1 ){
$counts[$keyl]=0;
}
else
$counts[$keyl]++;
$vlu=1;
}
$keyl++;
}

[edited by: aspr1n at 1:29 am (utc) on May 27, 2003]

ikbenhet1

1:28 am on May 27, 2003 (gmt 0)

10+ Year Member



Final try, before i give up.

I want to find the top 5 search phrases out of all the search queries a the txt file:

$tmparray=file("search.txt"); //load search queries

$tmparray[0]="food";
$tmparray[1]="big mac";
$tmparray[2]="pizza";
$tmparray[3]="big mac";
$tmparray[4]="ansjovis";
$tmparray[5]="pizza";
$tmparray[6]="toast";

the result i want:

"Most popular searches: 1.big mac, 2.pizza, etc "

aspr1n

1:58 am on May 27, 2003 (gmt 0)

10+ Year Member



ikbenhet1 pls stop editing large chunks out of previous posts as I'm trying to answer and finding your post has changed ;)

I was just working through your suggestion of using:

$vals = file( 'search.txt' );
$tmp = array_count_values( $vals );
$results = $result = array_unique ($tmp )

The problem I see with this is array_count_values will return an associative array based on the arrays keys name which I assume are your search strings, then you are array_unique()ing the search values rather than the strings (array keys).

If I were you I'd just keep it simple and do it the long way something like:

$vals = file( 'search.txt' ); 
$count = count( $vals );
for( $ei = 0; $ei < $count; $ei++ ) {
for( $i = 0; $i < $count; $i++ ) {
if ( $vals[ $i ] == $vals[ $ei ] )
$results[ $ei ][ "count" ]++;
else
$results[ $ei ] = $vals[ $i ];
}
}

You have to handle matching itself but that's just a jump.

asp