Jump to content

Trying to build a multidimensional array through a loop


mrbuter

Recommended Posts

First things first...my end goal is sorting some numbers I calculated from lowest to highest. From what I understand in order to do this I need to place them into an array first.

 

One other thing though, the numbers being calculated have an id related to them (it's a ratio for users)

 

The numbers are calculated through a for loop. After each iteration the number is stored in $ratio.

 

I can successfully echo the numbers corresponding to the users so I'm almost there, now I just need to sort it. The problem is I really don't know how to put the information into an array.

 

I tried doing something like this but it didn't worrk:

 

if ($x == 0) {
$info = Array(
	$x => Array
	(
	"" . $explode[$x] . "",
	"" . $ratio . ""
	)
} else if($x != $nbr_rows - 1) {
$info .=
	$x => Array
	(
	"" . $explode[$x] . "",
	"" . $ratio . ""
	),
}
else {
$info .=
	$x => Array
	(
	"" . $explode[$x] . "",
	"" . $ratio . ""
	)
	);
}

 

So say, after it completes all 3 iterations (realistically there will be about 70) it would look like this:

 

$info = Array(

[0] => Array (

[0] => 238492

[1] => 0.21

)

[1] => Array(

[0] => 100

[1] => 0.32

)

[2] => Array(

[0] => 819

[1] => 0.87

)

)

 

 

That way I would have the ratio that goes along with a unique id.

 

 

By all means, if you have any other way that I could sort these out that would be absolutely fine.

 

 

Thanks in advance.

 

//here we get all the ids for both the usergroups
$bootyusers_19 = mysql_query("
				SELECT userid
				FROM `user`
				WHERE `membergroupids` LIKE '%19%'
				");
$bootyusers_19_rows = mysql_num_rows($bootyusers_19);
$bootyusers_20 = mysql_query("
				SELECT userid
				FROM `user`
				WHERE `membergroupids` LIKE '%20%'
				");
$bootyusers_20_rows = mysql_num_rows($bootyusers_20);
$bootytotal = $bootyusers_19_rows + $bootyusers_20_rows; //and we add them together (effectively our number of rows for later on)


//now we need to implode both results into one set so we can make explode it and make it into an array
$i = 0;
$imploded = "";
while($row = mysql_fetch_array($bootyusers_19)){ // ($x=0;$x<$bootyusers_19_rows;$x++){ //and like $imploded += etc.
if ($i == 0) {
$imploded = $row['userid'];
} else {
$imploded .= "," . $row['userid'] . "";
}
$i++;
}

//now we slap on the usergroup id =20 users onto the back of it...still imploding of course
$i = 0;
while($row = mysql_fetch_array($bootyusers_20)){
$imploded .= "," . $row['userid'] . "";
$i++;
}

//weee exploded 
$exploded = explode(",", $imploded);


//now we do work
for ($x=0;$x<$bootytotal;$x++) {
//this query does leeches
$query = mysql_query(" 
	SELECT userid
		FROM post_thanks
		WHERE userid = $exploded[$x]
		ORDER BY userid DESC
		");
$rowsleech = mysql_num_rows($query);
$array = mysql_fetch_array($query); //we're not using this anymore

//this query does uploads
$query = mysql_query("
	SELECT postid
		FROM post AS post
		INNER JOIN thread AS thread ON(thread.threadid = post.threadid)
		WHERE post.userid = $exploded[$x]
			AND post.post_thanks_amount != 0
		");
$rowsupload = mysql_num_rows($query);
$array = mysql_fetch_array($query); //we're not using this anymore
if ($rowsleech == 0) {
$ratio = 0;
} else {
$ratio = round(($rowsupload / $rowsleech), 2);
}

$bootyuser[$x]['userid'] = $exploded[$x];
$bootyuser[$x]['leeches'] = $rowsleech;
$bootyuser[$x]['uploads'] = $rowsupload;
$bootyuser[$x]['ratio'] = $ratio;
//now we echo just to make sure it works (which it does)...........but how the hell do we sort it 
echo "Userid: " . $bootyuser[$x]['userid'] . ", number of leeches: " . $bootyuser[$x]['leeches'] . " and number of uploads: " . $bootyuser[$x]['uploads'] . " so the ratio for this user is " . $bootyuser[$x]['ratio'] . "<br>";
}

 

That's the rest of it in case it helps.

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.