简体   繁体   English

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

[英]PHP - Associative Array with 1 Key + 2 Values to HTML Table

I have an Associative Array with 1 Key and 2 Values Like this: 我有一个带有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);
   }
?>

The Input.txt contains: 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

I need to go through the array and display the results in a HTML table like this: 我需要遍历数组并将结果显示在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     |
+---------------------+------------------+------------+

How can This Be Done? 如何才能做到这一点?

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

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

If you want the domain as a single value, you should split the domain inside the while -loop from the user first: 如果要将域作为单个值,则应首先从用户处在while -loop内拆分域:

$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循环:

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: 您可以将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>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM