Jump to content

[SOLVED] associative arrays / sorting problems!


5pence

Recommended Posts

Hi Guys,

 

I am new to these pages so hello to everyone!

 

I have an associative array heres a small sample of it:

 

Array ( [/index.html] => 2001 12 [/date-availability.html] => 71 0 [/burner.html] => 31 0 [/jobs.html] => 101 0 [/index_bob.html] => 245 2 )

 

the keys are pages on the internet (/index.html)

the first number is hits 2001

the second number members 12

 

I want to sort this by members first AND if there are more than two cells that have equal number of members then those cells are sorted by number of hits. 

 

NB: The reason I am using an associative array is every visitor is recorded in mySQL, so using the landing page as a key I can easily count them:

 

while ( $record = mysql_fetch_row( $result )) {

 

// capture landing page string

$land = $record[2];

// capture record of member

$mem = $record[6];

 

// if association exists, increment count and member if necessary

if (isset($landings[$land])) {

$pieces = explode(" ", $landings[$land]);

$landings[$land] = $pieces[0]+1 . " " . $pieces[1]+=$mem;

}

// else, create new association, start count at 1 and member at 0

else if (!isset($landings[$land])) {

$landings[$land] = 1 . " " . $mem;

}

 

Thanks in advance!

5pence Spence

My table hold this:

 

   id  int(6)     No    auto_increment             

   refer  varchar(100)

   landing  varchar(100)             

   time  bigint(20)           

   date  date   

   ip  varchar(25)

   purchase  tinyint(1)     

   purchase_time  bigint

 

Every visit is recorded (im tracking) I want to make the information useful, This is the table i have created so far using a associative array and html:

 

Landing Page Instances No. of Conversions

/bookings.html      66             0

/index.html          2008          12

/date-availability.html 71         0

/burneras.html           31           0

/jobs.html            101          0

/index_.html    245          2

/gallery.html          59           0

/booking-printable.html 37        0

/about.html              93          2

 

I want the table to be sorted by no. of coversions then no of instances

 

thanks

 

try

<?php
$data = array (
        '/bookings.html'            => '66 0',
        '/index.html'               => '2008 12)',
        '/date-availability.html'   => '71 0', 
        '/burneras.html'            => '31 0', 
        '/jobs.html'                => '101 0', 
        '/index_.html'              => '245 2', 
        '/gallery.html'             => '59 0', 
        '/booking-printable.html'   => '37 0', 
        '/about.html'               => '93 2'  
    
);

function mysort($a, $b) {
     list($a1, $a2) = explode (' ', $a);
     list($b1, $b2) = explode (' ', $b);
     $a1 = intval($a1);
     $a2 = intval($a2);
     $b1 = intval($b1);
     $b2 = intval($b2);
     if ($a2 == $b2) {                  // if conversions are equal
        if ($a1==$b1) return 0;            // sort by instances
        return $a1 < $b1 ? 1 : -1;
     }
     return $a2 < $b2 ? 1 : -1;  
}

uasort($data, 'mysort');

/**
* check results
*/
echo '<pre>', print_r($data, true), '</pre>';
?>

 

-->[pre]

Array

(

    [/index.html] => 2008 12)

    [/index_.html] => 245 2

    [/about.html] => 93 2

    [/jobs.html] => 101 0

    [/date-availability.html] => 71 0

    [/bookings.html] => 66 0

    [/gallery.html] => 59 0

    [/booking-printable.html] => 37 0

    [/burneras.html] => 31 0

)

 

 

[/pre]

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.