0

我已经打了几次,只是想知道是否有人可能知道为什么会发生这种情况。

我传递的数组来自 WHM/cPanel API,格式为 (var_dump()ed):

array (size=14)
  '_diskquota' => string '262144000' (length=9)
  '_diskused' => string '31459' (length=5)
  'diskquota' => string '250' (length=3)
  'diskused' => string '0.03' (length=4)
  'diskusedpercent' => string '0' (length=1)
  'diskusedpercent20' => string '0' (length=1)
  'domain' => string 'xxxxxxx.co.uk' (length=20)
  'email' => string 'info@xxxxxxx.co.uk' (length=25)
  'humandiskquota' => string '250Â MB' (length=7)
  'humandiskused' => string '30.72Â KB' (length=9)
  'login' => string 'info@xxxxxxx.co.uk' (length=25)
  'mtime' => string '1347964089' (length=10)
  'txtdiskquota' => string '250' (length=3)
  'user' => string 'info' (length=4)

我正在将其转换为具有以下功能的 HTML 表格:

public function formatEmailAccountsArrayToTable( $email_accounts )  {
    $returnHTML = '';
    $topentag   =   '<table class="email_accounts_table">';
    $theader    =   '<thead><tr>
                        <th>Email</th><th>User</th><th>Domain</th><th>Disk Quota</th><th>Disk Used</th>
                        </tr></thead><tbody>';
    $tclosetag  =   '</tbody></table>';

    $returnHTML .= $topentag . $theader;

    foreach( $email_accounts as $v ) {
        $returnHTML .= '
            <tr>
                <td>' . $v['email'] . '</td>
                <td>' . $v['user'] . '</td>
                <td>' . $v['domain'] . '</td>
                <td>' . $v['diskquota'] . '</td>
                <td>' . $v['diskused'] . '</td>
            </tr>';
    }
    $returnHTML .= $tclosetag;

    return $returnHTML;
}

输出如下:

Email   User    Domain  Disk Quota  Disk Used
2   2   2   2   2
3   3   3   3   3
2   2   2   2   2
0   0   0   0   0
0   0   0   0   0
0   0   0   0   0
b   b   b   b   b
i   i   i   i   i
2   2   2   2   2
3   3   3   3   3
i   i   i   i   i
1   1   1   1   1
2   2   2   2   2
i   i   i   i   i

我看不出为什么任何值会被截断/转换为表中输出的值。

有谁知道为什么会发生这种情况,因为流程应该非常简单,但行为却很奇怪?

4

2 回答 2

3

Your $email_accounts array is a two-dimensional array, which you're iterating over in a foreach loop, at which point $v represents a single property of that array.

Ideally you need to restructure your array in order for the function to work correctly, a quickfix would be to do something like:

$email_accounts = array($email_accounts);

Which would give you:

array
  0 => 
    array
      '_diskquota' => string '262144000' (length=9)
      '_diskused' => string '31459' (length=5)
      'diskquota' => string '250' (length=3)
于 2012-09-18T10:48:41.950 回答
3

你的$email_accounts数组结构是这样的:

array(
    'x1' => 'y', 
    'x2' => 'y'
)

在您的代码中应该是这样的:

array(
    0 => array(
        'x1' => 'y', 
        'x2' => 'y'
    ), 
    1 => array(
        'x1' => 'y', 
        'x2' => 'y'
    )
)
于 2012-09-18T10:50:55.710 回答