By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,948 Members | 1,595 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,948 IT Pros & Developers. It's quick & easy.

Array help - best practice

100+
P: 348
I'd be interested in someone looking over this foreach loop to see if there is a better way to do this. These loops gives me exactly what I need but I can't help but think it isn't the best practice or even the right way of doing it.

The $reports variable is an array that holds data from my db. An example of what may be in the array is:

Expand|Select|Wrap|Line Numbers
  1. Report12    Title1    2    Administrator    test note    Deposit made    2009-05-03 04:01:22    1    Wilma    Ventura    1970-06-04
  2. Report12    Title1    2    Administrator    test note    Deposit made    2009-05-03 04:01:22    1    Jane    Williams    1967-07-11
  3. Report12    Title1    2    Administrator    test note    Deposit made    2009-05-03 04:01:22    1    Jack    Williams    1969-04-19
  4. Report12    Title1    2    Administrator    test note    Deposit made    2009-05-03 04:01:22    1    Bob    Savana    1959-09-15
  5.  
Hopefully, you can see that I have 4 rows where the first 8 columns are all exactly the same with the exception of the person's name and date of birth. What I need is to use this array to build an html report. In my code sample below, the outer loop gets only the first row to build the first page then the inner loop iterates over the people's names and date of birth

Can someone please tell me if there is a better way of doing this?

Expand|Select|Wrap|Line Numbers
  1.   $oldarr = $reports;
  2.   $newarr = array_splice($reports,0,1);
  3.   foreach($newarr as $val)
  4.   {
  5.     // my first 8 columns will go here
  6.     if($val['rpt_type'] == 'Report12')
  7.     {
  8.       foreach($oldarr as $rpt)
  9.       {
  10.         echo $rpt['fname'],$rpt['lname'],$rpt['dob'] . '<br>';
  11.       }
  12.     }
  13.   }
  14. }
  15.  
May 4 '09 #1
Share this Question
Share on Google+
3 Replies


Ciary
Expert 100+
P: 247
I'm not sure what you want, but if i understand you, you want something like this:
Expand|Select|Wrap|Line Numbers
  1. Report12    Title1    2    Administrator    test note    Deposit made    2009-05-03 04:01:22    1    Wilma    Ventura    1970-06-04
  2.  
  3. Wilma    Ventura    1970-06-04
  4. Jane    Williams    1967-07-11
  5. Jack    Williams    1969-04-19
  6. Bob    Savana    1959-09-15
if thats the case, i dont really see why you use array_splice. other then that, i think you're code is the best way to do it. i don't really see another.
May 4 '09 #2

100+
P: 348
fjm
@Ciary
Hi Ciary,

You're right, array_splice is not right. It is doing exactly what it should do but it's not right for what I need.

Let me try and explain again. Please look at the sample data below. In the example data below, there is only one actual report with 4 people associated. The 4 people associated with the report are what are causing all 4 rows to echo.

Each report has its own report number as the primary key in the database. If I could just loop off of that, I would be ok. Can someone please advise?


Expand|Select|Wrap|Line Numbers
  1. Report12    Title1    2    Administrator    test note    Deposit made    2009-05-03 04:01:22    1    Wilma    Ventura    1970-06-04
  2. Report12    Title1    2    Administrator    test note    Deposit made    2009-05-03 04:01:22    1    Jane    Williams    1967-07-11
  3. Report12    Title1    2    Administrator    test note    Deposit made    2009-05-03 04:01:22    1    Jack    Williams    1969-04-19
  4. Report12    Title1    2    Administrator    test note    Deposit made    2009-05-03 04:01:22    1    Bob    Savana    1959-09-15
May 4 '09 #3

Ciary
Expert 100+
P: 247
ok, but where is that different to what i suggested? i know another way though, with only one foreach.

Expand|Select|Wrap|Line Numbers
  1. $newest = "";
  2. foreach($newarr as $val){
  3.    if($val[0] != $newest){
  4.       $newest = $val[0];
  5.       echo $val[0]." ".$val[1]." ".$val[2]." ".$val[3]." ".$val[4]." ".$val[5]." ".$val[6]." ".$val[7].'<br><br>';
  6.    }
  7.    echo $val['fname'],$val['lname'],$val['dob'] . '<br>';
  8. }
  9.  
this will first show the 8 first Columns. then it will show a list with your users.
May 5 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.