ordering "rows" in a multi-dimensional array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jxn
Forum Newbie
Posts: 22
Joined: Wed Jul 13, 2005 4:33 pm

ordering "rows" in a multi-dimensional array

Post by jxn »

Hey,
I've got a very small bit of code which I use to store unique values in separate database rows or count the values up if they are not unique, making it easy for me to retrieve this data and ORDER BY the count values to gather frequency statistics; however, I'm trying to make it work without storing anything in a DB (I assume a multidimensional array would be the best bet)... and I know php has some functions for sorting arrays (which I hope would preserve the "rows" in multidimensional arrays), but I really don't know what I'm doing...and I'm just looking for some tips on which functions to use, how to go about it, or anything else really.

Here's the code I was using:

Code: Select all

//this neat little statement tells us, assuming correct spelling and proper input, approximately how many unique albums have been played this week; we'll need this to calculate frequency of each album's play.
$total_plays = mysql_fetch_array(mysql_query("SELECT COUNT(`album`) FROM `airplay`  WHERE `timestamp`>'$StatStartTime' "));
$unique_album_plays = mysql_fetch_array(mysql_query("SELECT COUNT(DISTINCT LOWER(`album`)) FROM `airplay` WHERE `timestamp`>'$StatStartTime' "));
$total_new_plays = mysql_fetch_array(mysql_query("SELECT COUNT(DISTINCT `cddbid`) FROM `airplay` WHERE `status`='2' AND `cddbid`!='' AND `timestamp`>'$StatStartTime' "));

//
// we have a special temporary table where we're going to store data we collect in the next statement
//

// because this table is temporary, we should make sure it's tabula rasa when we use it.
mysql_query("DELETE FROM `temp_stat` WHERE `id`>'-1'") or die(mysql_error()); 

//this statement will grab all the CDs that we care about, new ones... we wont be charting anything else, obviously
$stat_query = "SELECT * FROM `airplay` WHERE `status`<='2' AND `timestamp`>'$StartStatTime' AND `cddbid`!='' ";
$stat_result = mysql_query($stat_query);
while($stat_row = mysql_fetch_assoc($stat_result)) {
	$temp_query = "SELECT * FROM `temp_stat` WHERE `cdid`='".$stat_row[cddbid]."' LIMIT 1";
	$temp_result = mysql_query($temp_query) or die(mysql_error());	
	if ($temp_row = mysql_fetch_assoc($temp_result)) {
		$spins = $temp_row[spins];
		$spins++;
		mysql_query("UPDATE `temp_stat` SET `spins`='".$spins."' WHERE `cdid`='".$stat_row[cddbid]."' ") or die(mysql_error());
	} else {
		mysql_query("INSERT INTO `temp_stat` SET `spins`='1', `cdid`='".$stat_row[cddbid]."' ") or die(mysql_error());
	}
}
$rating = 0;
$statistics_query = "SELECT * FROM `temp_stat` ORDER BY `spins` DESC";
$statistics_result = mysql_query($statistics_query) or die(mysql_error());
while ($statistics_row = mysql_fetch_assoc($statistics_result)) {
++$rating;
if (($rating - 1) <= ($total_new_plays[0] / 3)) {
	$heavy_spins = $statistics_row[spins];
} elseif (($rating - 1) <= ($total_new_plays[0] / 1.5)) {
	$medium_spins = $statistics_row[spins];
} 
}
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

This seems pretty complex. I'm too tired at the moment to think of a logical code for you, but you can look into array_keys(), array_flip(), sort(), ksort(), and usort()
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
dreamline
Forum Contributor
Posts: 158
Joined: Fri May 28, 2004 2:37 am

Post by dreamline »

Here's what i've found on php.net in the comments, i've tested it and sofar it works for me.. :)

hxxp://nl2.php.net/manual/en/function.usort.php
(Post 20-Jul-2005 10:56)

Code: Select all

<?php
$test = array();
$test[0]['name'] = 'jeremy';
$test[0]['email'] = 'lala@fishies.com';
$test[0]['phone'] = '123-123-1234';
$test[0]['trick'] = 'mezopia';

$test[1]['name'] = 'Amanda';
$test[1]['email'] = 'hot@hotmail.com';
$test[1]['phone'] = '123-123-1235';
$test[1]['trick'] = 'youarecool';

$test[2]['name'] = 'john';
$test[2]['email'] = 'wowee@yahoo.com';
$test[2]['phone'] = '123-123-3333';
$test[2]['trick'] = 'goneinanhour';

print_r(columnSort($test, 'name'));

function columnSort($unsorted, $column) {
   $sorted = $unsorted;
   for ($i=0; $i < sizeof($sorted)-1; $i++) {
     for ($j=0; $j<sizeof($sorted)-1-$i; $j++)
       if ($sorted[$j][$column] > $sorted[$j+1][$column]) {
         $tmp = $sorted[$j];
         $sorted[$j] = $sorted[$j+1];
         $sorted[$j+1] = $tmp;
     }
   }
   return $sorted;
}
?>
Post Reply