![]() |
Assigning variables and their values from ARRAYS
I have the following array from a mysql_query():
Array ( [0] => 1 [1] => 4 ) Array ( [0] => 2 [1] => 19 ) Array ( [0] => 4 [1] => 18 ) Array ( [0] => 5 [1] => 5 ) Array ( [0] => 6 [1] => 7 ) Array ( [0] => 7 [1] => 19 ) Array ( [0] => 9 [1] => 6 ) Numbers 1 - 9 represent math instructional units, and numbers 4, 19, 2, etc., represent the number of test items within each unit. Note that there are no units for 3 and 8. From the array, I want to derive a group of variables (units) and assign them to the number of items in each group. Example: $mUnit1 = 4 $mUnit2 = 19 $mUnit4 = 18.....etc. I've tried and tried...and tried...but I don't have the chops to do this. (At the rate I'm going, I will by 2012.) Any help would be appreciated! Dave |
I need more info like:
1. the code you have that retrieved those 5 separate array's from the DB,. 2. why are you getting 5 separate arrays from the DB? the only easy way i can see is by: PHP Code:
|
Is it a multi-dimensional array?
$aArray = array([0] => array ([0] => 1, [1] => 4), [1] => array([0] => 2, [1] => 19) ...or is there actually a seperate array for each result set? -m |
According to his notation he is getting separate, which is a bit odd.
After alittle more thought i came up with this convoluted script (providing you are getting 7 separate arrays): WARNING: hackish code, it may blind you! YOU HAVE BEEN WARNED! PHP Code:
Unit1: 4 Unit2: 19 Unit4: 18 Unit5: 5 Unit6: 7 Unit7: 19 Unit9: 6 there are a few things about this however: 1. eval is slow and can be a security risk 2. you are limited to 7 arrays unless you manually change the loop 3. its barely readable 4. i have a reasonably large headache now I think the better way is to re-think how you retrieve your data (assuming this is how you are retrieving it currently, i apologise if i have misunderstood). |
Thanks to all who replied...
Just to answer the question about the nature of the query and what I am trying to do, here goes: I have a table that contains an "answer key" for a given test. There is a row for each test item. The fields in the table include, among others, "munit" (instructional unit for each math item). The values in the field can vary widely from test-to-test, since not all math instr. units have to be represented on every test. Thus, I executed a query that gave me, for this particular test, the "munits" that are represented on the test, as well as the number of items within each "munit". The query was: SELECT munit, count(munit) AS mvar FROM keytable GROUP BY munit; If I do a print_r, here are the array results (with no formatting): Array ( [0] => 1 [1] => 4 ) Array ( [0] => 2 [1] => 19 ) Array ( [0] => 4 [1] => 18 ) Array ( [0] => 5 [1] => 5 ) Array ( [0] => 6 [1] => 7 ) Array ( [0] => 7 [1] => 19 ) Array ( [0] => 9 [1] => 6 ) So, when the test items are scored, I can compute the percent correct for each applicable "munit". Thus, I need a series of variables, such that... $mUnit1 = 4, $mUnit2 = 19, etc... Does this make sense? Again, I appreciate the help I've already received. Thanks! |
UPDATE...A few hours later...
OK, this is what I've come up with so far...
CODE: PHP Code:
Results: $munit1 = 4 $munit2 = 19 $munit4 = 18 $munit5 = 5 $munit6 = 7 $munit7 = 19 $munit9 = 6 My problem is, "How can I get these assignments to be an actual part of the program; that is, to be 'real' variables with their associated 'real' values?" Do you see what I mean? Outside of the above WHILE loop, "$munit1" has no meaning. I want the variables to go into memory so that they can be used as part of the execution of the program. Probably not making much sense, but there you are. Dave |
as i said before, a variable variable will do this:
PHP Code:
hope that helps. |
The use of discrete global variables for something like this seems a little excessive. The data pattern is that of a simple array with
$munitn being associated with some value. I can't see why using many different variables would be preferred over a simple array.PHP Code:
|
I agree, but that's what he wanted.
|
Thanks for the updates...
Again, thanks to sketchMedia and Salathe for your replys.
I am in the process of studying everything that you've suggested. Thanks again! Dave |
| All times are GMT. The time now is 05:22 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0