1

I'm relatively new to PHP and I hope you can help me solve my problem. I am selecting out data from a database into an array for timekeeping. Ultimately, I would like to calculate the total number of hours spent on a project for a given customer.

Here is the code to populate a multi-dimensional array: ...

foreach ($record as $data) {  
  $mArray = array();  
  $name = $data['user'];  
  $customer = $data['customer'];  
  $project = $data['project'];  
  $hours = $data['hours'];  

  $mArray[$name][$customer][$project] += $hours;  
}

...

I would now like to iterate over $mArray to generate an xml file like this:

...

foreach ($mArray as $username) {
  foreach ($mArray[$username] as $customerName) {
    foreach ($mArray[$username][$customerName] as $project ) {
        echo '<'.$username.'><'.$customerName.'><'.$project.'><hours>'.
           $mArray[$username][$customerName][$project].'</hours></'.$project.'>
           </'.$customerName.'></'.$username.'>';
    }
  }
}

This nested foreach doesn't work. Can someone give me a couple of tips on how to traverse this structure? Thank you for reading!

UPDATE: Based on the comments I've received so far (and THANK YOU TO ALL), I have:

foreach ($mArray as $userKey => $username) {
  foreach ($mArray[$userKey] as $customerKey => $customerName) {
    foreach ($mArray[$userKey][$customerKey] as $projectKey => $projectName) {
      echo '<name>'.$userKey.'</name>';
      echo "\n";
      echo '<customerName>'.$customerKey.'</customerName>';
      echo "\n";
      echo '<projectName>'.$projectKey.'</projectName>';
      echo "\n";
      echo '<hours>'.$mArray[$userKey][$customerKey][$projectKey].'</hours>';
      echo "\n";
    }
  }
}

This is now only providing a single iteration (one row of data).

1 Answer 1

5

Foreach syntax is foreach($array as $value). You're trying to use those values as array keys, but they're not values - they're the child arrays. What you want is either:

foreach($mArray as $username) {
   foreach($username as ...)

or

foreach($mArray as $key => $user) {
   foreach($mArray[$key] as ...)
Sign up to request clarification or add additional context in comments.

6 Comments

I'm getting closer. I chose your option 2. But now, I'm only getting the first set of data.
probably would be better to use something like foreach($mArray as $username=>$customer){ foreach($customer as $customerName=>$projects){ foreach($project as $project=>$hours){ ... then you have access to the keys as $username, $customerName, $project and the $hours
make sure you use different names for all your $x => $y variables, or your inner loops will overwrite whatever the outer loops are doing.
Yes. My syntax is correct now, but I'm only getting one row:
foreach ($mArray as $userKey => $username) { foreach ($mArray[$userKey] as $customerKey => $customerName) { foreach ($mArray[$userKey][$customerKey] as $projectKey => $projectName) { echo '<name>'.$userKey.'</name>'; echo "\n"; echo '<customerName>'.$customerKey.'</customerName>'; echo "\n"; echo '<projectName>'.$projectKey.'</projectName>'; echo "\n"; echo '<hours>'.$mArray[$userKey][$customerKey][$projectKey].'</hours>'; echo "\n"; } } }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.