Multi Dimensional array issue |
[eluser]RichM[/eluser]
I've been having a couple issues with finalizing some code. It's very close but not quite there and I'm stumped. I haven't worked with multi-dimensional arrays much so go easy on me. I have a result from a database query that outputs multiple rows of equipment by joining 2 tables together. As you can see below it is sorted by eid ASC, pid ASC, and then hdate ASC. Each piece of equipment may have an hdate with associated hours on that date. For instance, eid of 54 has several dates with hours logged for pid 1 and then another date/hours with pid 4. Result: Code: eid | cid | make | model | name | eqid | pid | hdate | hours | uid To output properly to my view (and eventually a spreadsheet) I would like it to output the array with a row for each piece of equipment for each project with a subarray of the dates and associated hours. Something like below where I'll loop through each array and if it has dates would loop through those and place the hours in the proper day of the month. Required Output: Code: Array The code below outputs an unindexed array of exactly what the result above is. Been reading posts here and stackoverflow, but haven't had much luck getting it to output properly. Code: Code: $equip=array(); Any help is much appreciated.
[eluser]jmadsen[/eluser]
this mihgt be of some help: http://www.codebyjeff.com/blog/2012/08/n...rscore-php uses the Underscore.php library to group on keys
[eluser]fasfad[/eluser]
I've made an example, not tested. (should work) ;-) Code: $equipment = array(); Your result would be: Code: //You should get something like this:
[eluser]RichM[/eluser]
That's perfect as far as output goes. Very clear thought process on what I was wanting - I would have never thought of formatting by projects. The only issue I'm having now is something with the output. When I print_r or var_dump it doesn't look to be indexing each equipment array. The projects array is indexed properly with pid. I just added the eid to be included in the array so it's usable like the rest of the items. You've given me a way better understanding of multi dimensional arrays than what I've read anywhere else. And maybe that's because I can relate it more directly to my issue. Many thanks.
[eluser]fasfad[/eluser]
No Problem, it's all about making a picture in your head of the ouput you want and nest the array according to that picture. Just something I thought about, I thought you may also want to sort the array according to the equipment usage per project. So you might want to create another dimension, so change the code to get something like this ![]() Code: Array EDIT: I changed some things to get the above output: Code: //some testdata Code: array(2) { Code: foreach($output["projects"] as $pid=>$project){ Project #1 , equipment used: - Clark -2012-11-07: 5hours -2012-11-08: 2hours -2012-11-09: 4hours Project #4 , equipment used: - Clark -2012-11-10: 7hours - Toyota -2012-11-07: 5hours
[eluser]fasfad[/eluser]
Edited my post , not sure if it notifies you, because it had some bad code ![]()
[eluser]RichM[/eluser]
Interesting. That actually aligns well with another report that I am needing to do that outputs per project. Essentially each user has access to just some of the projects so I could limit the output to just their jobs and sort equipment and usage that way.
[eluser]RichM[/eluser]
Ok, back to the original query/output. I've got it outputting properly except for 'some' of the dates. I'm returning the equipment array as Code: $output as $line Code: while($hd<=$days) The problem is that it only works for 2 digit days, of course. So 2012-11-1 is not a proper index as it should be 2012-11-01. Once I hit 2012-11-10 it works properly as you would expect. Is there a way to force the $hd variable to be 2 digit? I'm sending the $days variable into it with the function days_in_month() and passing the month and year. Or maybe I'm going all the way back to how the dates input into the database? I'm using jqueryUI's datepicker for the date. I'm thinking I'll just modify the format of the original entry. I'll say thanks again. The information you've given me has been spot on and has fit perfectly.
[eluser]RichM[/eluser]
Well database won't allow me to save as a single digit without going to just saving as a string. But then the query won't work properly. Ended up using str_pad() and it's working. |
Welcome Guest, Not a member yet? Register Sign In |