Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an Associative Array with 1 Key and 2 Values Like this:

<?php
// file path 
$file = 'input.txt';  // REMEMBER TO MENTION THE CORRECT FILE WITH EXTENSION
// open the file and get the resource handle with errors suppressed 
$handle = @fopen($file,'r');  // DONT USE @ while at development since it will suppress     errors
// array to hold our values 
$params = array(); 
if($handle) 
{ 
// if handle is there then file was read successfully 
// as long as we aren't at the end of the file 
   while(!feof($handle)) 
   { 
       $line = fgets($handle); 
       $temp = explode(':',$line); 
       $params[$temp[0]][] = $temp[1]; 
       $params[$temp[0]][] = $temp[2]; 
   } 
   fclose($handle);
   }
?>

The Input.txt contains:

key:Value1:Value2

[email protected]:1:11    
[email protected]:2:12    
[email protected]:3:13    
[email protected]:4:14

I need to go through the array and display the results in a HTML table like this:

+-----------------------------------------------------+
|                     example.com                     |
+---------------------+------------------+------------+
| [email protected]   |         1        |      11    |
| [email protected]   |         4        |      14    |
+---------------------+------------------+------------+
|                     test.com                        |
+---------------------+------------------+------------+
| [email protected]      |         3        |     13     |
| [email protected]      |         2        |     12     |
+---------------------+------------------+------------+

How can This Be Done?

share|improve this question
    
Mark an answer as accepted.. –  1nflktd Jun 5 at 23:09
    
Thanks Everyone For your help :-) –  user3706271 Jun 7 at 9:09

3 Answers 3

up vote 0 down vote accepted

If you want the domain as a single value, you should split the domain inside the while-loop from the user first:

$line = fgets($handle); 
$temp = explode(':',$line);
$mail = explode('@', $temp[0]);
$params[$mail[1]]             = array();  // Initialize the array first, otherwise
$params[$mail[1]][$mail[0]]   = array();  // this could throw a warning/notice
$params[$mail[1]][$mail[0]][] = $temp[1]; 
$params[$mail[1]][$mail[0]][] = $temp[2]; 

Then you can use a foreach loop:

foreach ( $params as $domain => $user) {
    echo $domain;
    foreach ( $user as $mail => $data ) {
        echo $mail . "@" . $domain;
        echo $data[0];
        echo $data[1];
    }
}

This of course needs a little formatting. You can echo an HTML table along with the data:

echo "<table>";
foreach ( $params as $domain => $user) {
    echo "<tr><td colspan=3>" . $domain . "</td></tr>";
    foreach ( $user as $mail => $data ) {
        echo "<tr>";
        echo "<td>" . $mail . "@" . $domain . "</td>";
        echo "<td>" . $data[0] . "</td>";
        echo "<td>" . $data[1] . "</td>";
        echo "</tr>";
    }
}
echo "</table>";
share|improve this answer

I changed it a little bit:

if($handle) 
{ 
    $array = array ();
    while(!feof($handle)) 
    { 
       $line = fgets($handle); 
       $temp = explode(':',$line);
       $tempArray = array ();
       $tempArray[0] = temp[0];
       $tempArray[1] = temp[1];
       $tempArray[2] = temp[2];
       $key = explode("@", temp[0]);
       $array[$key[0]][] = $tempArray;
    } 
    fclose($handle);
}

$table = "<table>";
foreach($array as $key => $value){
   $table .= "<tr><th colspan='3'>".$key."</th></tr>";
   foreach($value as $v){
        $table .= "<tr><td>".$v[0]."</td><td>".$v[1]."</td><td>".$v[2]."</td></tr>";
   }
}
$table .= "</table>";
share|improve this answer

I would rather use this structure:

if($handle) 
{ 
    // if handle is there then file was read successfully 
    // as long as we aren't at the end of the file 
    while(!feof($handle)) 
    { 
        $line = fgets($handle); 
        $temp = explode(':',$line);
        $user_data = explode('@', $temp[0]);
        $params[user_data[1]][$temp[0]][] = $temp[1]; 
        $params[user_data[1]][$temp[0]][] = $temp[2]; 
    } 
    fclose($handle);
}

And PHP + HTML:

<?php
$return = '<table>';

foreach($params as $key => $value)
    $return .= '<tr><td colspan="3">'.$key.'</td></tr>';
    foreach ($value as $user => $user_data) {
        $return .= '<tr><td>'.$user.'</td><td>'.$user_data[0].'</td><td>'.$user_data[1].'</td></tr>';
    }
$return = '</table>';
}
?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.