PHP - 具有1个键的关联数组+ HTML表的2个值

时间:2014-06-05 12:42:31

标签: php arrays key associative-array

我有一个带有1个键和2个值的关联数组,如下所示:

<?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);
   }
?>

Input.txt包含:

key:Value1:Value2

test1@example.com:1:11    
test2@test.com:2:12    
test3@test.com:3:13    
test4@example.com:4:14

我需要遍历数组并在HTML表格中显示结果:

+-----------------------------------------------------+
|                     example.com                     |
+---------------------+------------------+------------+
| test1@example.com   |         1        |      11    |
| test4@example.com   |         4        |      14    |
+---------------------+------------------+------------+
|                     test.com                        |
+---------------------+------------------+------------+
| test3@test.com      |         3        |     13     |
| test2@test.com      |         2        |     12     |
+---------------------+------------------+------------+

怎么做到这一点?

3 个答案:

答案 0 :(得分:0)

我宁愿使用这种结构:

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);
}

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>';
}
?>

答案 1 :(得分:0)

我稍微改了一下:

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>";

答案 2 :(得分:0)

如果您希望将域作为单个值,则应首先从用户首先在while - 循环中拆分域:

$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]; 

然后你可以使用foreach循环:

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

这当然需要一点格式化。您可以回显HTML表格以及数据:

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>";