Advanced Array Extrapolation
Moderator: General Moderators
-
t3hm4d0n3
- Forum Newbie
- Posts: 4
- Joined: Sun Jun 12, 2005 3:13 am
- Location: Glenwood springs
- Contact:
Advanced Array Extrapolation
Here is my problem,
I believe it is rather unique, i understand that there are many techniques
to get data out of a multidimensional array, however this has a few other
factors that are not associated with just extracting from an array.
I have an array it is many dimensions deep with 2 more dimensions within a
dimension.
using two count calls, and a two for statements you would run through it all,
and extracting the data would look like this:
$Returned_data[$t][0][0][$b][0];
where $t is the first key basically the id of each array set.
and $b is the count containing the id of each sub array set
within $b is another 2 keys.
$Returned_data[$t][0][0][$b]=>([0][1])[1-0];
the data i really need is within the ([0]&[1])[$b] keys stored in
[$b][0] and [$b][1]
the rest of the keys are refrences to get to that point.
key [0][$b][0] is a descriptor
and
key [0][$b][1] is a number
and
key [1][$b][1] is the max amount that [0][$b][1] can be
i need to figure out how to extract that data efficiently
and then output the numbers into a table with
$amt = $Returned_data[$t][0][1][$b][1] / 2;
$amt = $Returned_data[$t][0][1][$b][1] / $amt;
number of columns..... i think
thats of course using !is_float($Returned_data[$t][0][1][$b][1] / 2)
to make sure it will be divisable.
then i need to put the numbers in their corresponding places
with a descriptor in the first column of each row.
so if the max is 10 and there are a 4 6 and 9 then the nine goes into
the column in 9-10 and the four goes into the column 3-4 and so on.
actually what i really need it to do is give me a percentage. so what
percent is in the 5-6 column and what percent is in the 7-8 column.
and also a total of the numbers below that.
this data is pulled from a database, as an array, but within the array
is a string of data that needs to be exploded, and that is
where you get $b, containing the data.
this already puts an immense load on the server because there is quite a lot of
data there. very man individual cases, and each of these cases is found using a joined query on another table in the database as a refrence. so its a bit taxing on the server.
if i could get a little input that would rock,
maybe some ideas or possibly another way to approach this whole thing?
because i am just burnt out on the whole thing,
i need some fresh input.
some theories on how this could be approached.
thanks,
-=Levi=-
I believe it is rather unique, i understand that there are many techniques
to get data out of a multidimensional array, however this has a few other
factors that are not associated with just extracting from an array.
I have an array it is many dimensions deep with 2 more dimensions within a
dimension.
using two count calls, and a two for statements you would run through it all,
and extracting the data would look like this:
$Returned_data[$t][0][0][$b][0];
where $t is the first key basically the id of each array set.
and $b is the count containing the id of each sub array set
within $b is another 2 keys.
$Returned_data[$t][0][0][$b]=>([0][1])[1-0];
the data i really need is within the ([0]&[1])[$b] keys stored in
[$b][0] and [$b][1]
the rest of the keys are refrences to get to that point.
key [0][$b][0] is a descriptor
and
key [0][$b][1] is a number
and
key [1][$b][1] is the max amount that [0][$b][1] can be
i need to figure out how to extract that data efficiently
and then output the numbers into a table with
$amt = $Returned_data[$t][0][1][$b][1] / 2;
$amt = $Returned_data[$t][0][1][$b][1] / $amt;
number of columns..... i think
thats of course using !is_float($Returned_data[$t][0][1][$b][1] / 2)
to make sure it will be divisable.
then i need to put the numbers in their corresponding places
with a descriptor in the first column of each row.
so if the max is 10 and there are a 4 6 and 9 then the nine goes into
the column in 9-10 and the four goes into the column 3-4 and so on.
actually what i really need it to do is give me a percentage. so what
percent is in the 5-6 column and what percent is in the 7-8 column.
and also a total of the numbers below that.
this data is pulled from a database, as an array, but within the array
is a string of data that needs to be exploded, and that is
where you get $b, containing the data.
this already puts an immense load on the server because there is quite a lot of
data there. very man individual cases, and each of these cases is found using a joined query on another table in the database as a refrence. so its a bit taxing on the server.
if i could get a little input that would rock,
maybe some ideas or possibly another way to approach this whole thing?
because i am just burnt out on the whole thing,
i need some fresh input.
some theories on how this could be approached.
thanks,
-=Levi=-
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
-
t3hm4d0n3
- Forum Newbie
- Posts: 4
- Joined: Sun Jun 12, 2005 3:13 am
- Location: Glenwood springs
- Contact:
lol, alrighty, well thanks anyways,
I cant really do it any other way though.
its a bit on the complicated side, i could just put the data into seperate arrays, but yet again it would take alot of time to parse through and extract what was needed from each array.
if i pulled each set of data for each report using mysql queries and only turned the actual score data into an array, it would be too many sql queries which would slow everything down.
so i pulled everything using one sql query, and then put the data into a large array.
maybe if i post the code that creates the array, you might be able to see a better strategy for doing it , something i have missed or just didnt think of.
Code: Select all
function Return_stu_data($school, $teacher_name)
{
$query = "SELECT DISTINCT FABASTU.SSTU
FROM ((( FABATCH INNER JOIN FABAMST ON FABATCH.TTCH = FABAMST.MTCH
) INNER JOIN FABACLS ON FABAMST.MSEC = FABACLS.RSEC ) INNER JOIN FABACOR ON
FABAMST.MCOR = FABACOR.CCOR)
INNER JOIN FABASTU ON FABACLS.RSTU = FABASTU.SSTU WHERE FABASTU.SSCH = '".$school."'
AND FABATCH.fullname = '".$teacher_name."' ORDER BY FABASTU.SSNM";
$tch_data = mysql_fetch_all($query, "ILP");
for($s=0;$s<count($tch_data);$s++)
{
$stu_query = "SELECT * FROM student where stu_ID = ".$tch_data[$s]['SSTU']."";
$stu_data = mysql_fetch_all($stu_query);
if(count($stu_data) > 2 || count($stu_data) < 2)
{
}
else
{
for($i=0;$i<count($stu_data);$i++)
{
if($stu_data[$i]['semester'] == "fall")
{
$assess_score_data =
explode("^", $stu_data[$i]['assess_score']);
for($t=0;$t<count($assess_score_data);$t++)
{
$assess_score_type[$t] = explode("|", $assess_score_data[$t]);
}
for($d=0;$d<count($assess_score_type);$d++)
{
$assess_score_type_area[0][$d] =
explode(":", $assess_score_type[$d][0]);
$assess_score_type_area[1][$d] =
explode("%", $assess_score_type[$d][1]);
}
$returned_data[$s][$i] = $assess_score_type_area;
}
if($stu_data[$i]['semester'] == "spring")
{
$assess_score_data = explode("^", $stu_data[$i]['assess_score']);
for($t=0;$t<count($assess_score_data);$t++)
{
$assess_score_type[$t] = explode("|", $assess_score_data[$t]);
}
for($d=0;$d<count($assess_score_type);$d++)
{
$assess_score_type_area[0][$d] =
explode(":", $assess_score_type[$d][0]);
$assess_score_type_area[1][$d] =
explode("%", $assess_score_type[$d][1]);
}
$returned_data[$s][$i] = $assess_score_type_area;
}
}
}
}
return $returned_data;
}i appreciate any help,
-=Levi=-
There's far too much going on in that function. This makes it difficult to see what's going on. It really ought to refactored into several functions, (or better still classes) each doing just one thing.
You can get into a tangle in programming at the drop of a hat. The key to avoiding this is to think in terms of application layers. The standard way to conceive of an application is in three layers: presentation, domain and data access.
Presentation is the UI. The presentation layer receives input, decides what sort of response to make, manipulates the domain and creates a web page (with http output). With a clearly defined presentation layer, the whole thing can be swapped out easily eg if you want to produce a CLI version of the app.
The data access layer gets raw data from persistent storage. This is usually a database but could be something else such as flat file. Again, a clearly defined data access layer makes it easy to change the storage mechanism without affecting other parts of the app.
The domain is the business logic. The domain layer receives raw data from persistent storage and "adds value" to it. This might be, for example, calculating the interest due on an account. These are business rules ie the real meat and veg of the application. In simple cases, there is no domain logic to do and raw data is simply served up to the display mechanism.
Each layer has free access to the one below it but not vice versa. For example the data access layer has no knowledge of the domain and the uses to which the data it provides is put. In turn, the domain has no knowledge of the presentation.
So, would you like to take a stab at splitting the above code up into different responsibilities? Don't worry if this is all new to you. Just take your best shot.
You can get into a tangle in programming at the drop of a hat. The key to avoiding this is to think in terms of application layers. The standard way to conceive of an application is in three layers: presentation, domain and data access.
Presentation is the UI. The presentation layer receives input, decides what sort of response to make, manipulates the domain and creates a web page (with http output). With a clearly defined presentation layer, the whole thing can be swapped out easily eg if you want to produce a CLI version of the app.
The data access layer gets raw data from persistent storage. This is usually a database but could be something else such as flat file. Again, a clearly defined data access layer makes it easy to change the storage mechanism without affecting other parts of the app.
The domain is the business logic. The domain layer receives raw data from persistent storage and "adds value" to it. This might be, for example, calculating the interest due on an account. These are business rules ie the real meat and veg of the application. In simple cases, there is no domain logic to do and raw data is simply served up to the display mechanism.
Each layer has free access to the one below it but not vice versa. For example the data access layer has no knowledge of the domain and the uses to which the data it provides is put. In turn, the domain has no knowledge of the presentation.
So, would you like to take a stab at splitting the above code up into different responsibilities? Don't worry if this is all new to you. Just take your best shot.
-
t3hm4d0n3
- Forum Newbie
- Posts: 4
- Joined: Sun Jun 12, 2005 3:13 am
- Location: Glenwood springs
- Contact:
10-4
Thanks, that is a very good idea,
I did let it a bit hairy there didnt I?
I was basically using this function as part of the data access layer,
it doesnt really do anything with the data but get it and put it into an array.
Thanks for that tip though, thats a very quick and concise way of putting how to go about
splitting up your functions.
We'll see, i've been working on it so i may yet come up with a better way to do it.
thanks for your input.
-=Levi=-
I did let it a bit hairy there didnt I?
I was basically using this function as part of the data access layer,
it doesnt really do anything with the data but get it and put it into an array.
Thanks for that tip though, thats a very quick and concise way of putting how to go about
splitting up your functions.
We'll see, i've been working on it so i may yet come up with a better way to do it.
thanks for your input.
-=Levi=-
-
t3hm4d0n3
- Forum Newbie
- Posts: 4
- Joined: Sun Jun 12, 2005 3:13 am
- Location: Glenwood springs
- Contact:
It was pretty simple after all
Well it seems that the solution was only a few lines of code.
Very simple, i just had to reverse my cranial rectal inversion and all was not lost.
basically 2 foreach and one for statment solved the problem.
as you can see, very simple to solve a complex array.
it really helps to write it out and break it into parts, really solves the confusion.
also figured out the best way to make a number sets.
ie: |0-5| |6-10| |11-20| |21-25|
this is in the case of multiple max numbers.
so it will figure out the best way to break them up into groups.
this would be used in the case of scores, such as this many students scored between 11-20 and so on.
this is a bit shorter version, but it gets the point across.
then basically you take your output, and then your scores (in an array), explode $output into an array.
then use a for loop with the min and max in output with in_array() to find out how many are within a specific range.
thanks for your input it helped to rethink some things in the matter of using more functions.
-=Levi=-
Very simple, i just had to reverse my cranial rectal inversion and all was not lost.
basically 2 foreach and one for statment solved the problem.
Code: Select all
function sort_return_data($array)
{
$numCount = 0;
foreach($array as $results=>$season)
{
$count = count($season);
for($a=0;$a<$count;$a++)
{
$a_score = $season[$a][1];
foreach($a_score as $stu_score)
{
$numCount++;
$max_score .= "".$stu_score[1].",";
if($stu_score[0] == "")
{
$score_data .= "0,";
}
else
{
$score_data .= "".$stu_score[0].",";
}
}
$score_data = rtrim($score_data, ",");
$max_score = rtrim($max_score, ",");
$sorted_data = sort_data($max_score, $score_data,$numCount);
$numCount = NULL;
$max_score = NULL;
$score_data = NULL;
}
}
return $sorted_data;
}it really helps to write it out and break it into parts, really solves the confusion.
also figured out the best way to make a number sets.
ie: |0-5| |6-10| |11-20| |21-25|
this is in the case of multiple max numbers.
so it will figure out the best way to break them up into groups.
this would be used in the case of scores, such as this many students scored between 11-20 and so on.
Code: Select all
$values = range(0, $data2chunk);
$chunkamt = round($data2chunk/10); //10 being the number of sets you want.
$rows = array_chunk($values, $chunkamt);
for($i=0;$i<count($rows);$i++)
{
$max_count = count($rows[$i]);
$output .= "".$rows[$i][0]."-".$rows[$i][$max_count-1].",";
}then basically you take your output, and then your scores (in an array), explode $output into an array.
then use a for loop with the min and max in output with in_array() to find out how many are within a specific range.
thanks for your input it helped to rethink some things in the matter of using more functions.
-=Levi=-