PHP 需要将嵌套数组值转储到 html table 单元格中

PHP Need nested array values to dump into html table cell

我有一个带有嵌套用户数组的 stdClass 对象数组作为我想在 table 单元格中显示的值。

我很挣扎,已经耗尽了很多时间。如果可能的话,我希望能朝着正确的方向前进。

下图将显示我正在尝试做的事情。

$someArray = json_decode($token_result);

foreach ($someArray as $key => $value) {
    $combined[] =
        "<tr><td>" .
        $value->customer .
        "</td><td>" .
        $value->city .
        '</td>
           <td>' .
        $value->state .
        "</td><td>" .
        $value->zipcode .
        '</td>
           <td>' .
        $value->totalUsers .
        "</td><td>" .
        $value->totalActiveUsers .
        "</td><td>" .
        $value->totalInActiveUsers .
        '</td><td>
           <a href="">' .
        $value->users .
        "</a></td></tr>";
}

foreach ($combined as $value) {
    print $value;
}

这是我的数组和对象的样子

Array
(
    [0] => stdClass Object
        (
            [customer] => SWSH
            [city] => Thomasville
            [state] => GA
            [zipcode] => 31792
            [totalUsers] => 6
            [totalActiveUsers] => 6
            [totalInActiveUsers] => 0
            [users] => Array
                (
                    [0] => stdClass Object
                        (
                            [firstName] => xxx
                            [lastName] => xxx
                            [phoneNumber] => 
                            [userName] => cb_igwrth@xxx.com
                            [createdBy] => 
                            [isActive] => 1
                        )

                    [1] => stdClass Object
                        (
                            [firstName] => Dan
                            [lastName] => Stewart
                            [phoneNumber] => +11111111111
                            [userName] => dan.sxx@xxx.ga.gov
                            [createdBy] => kwilliams@xxx.com
                            [isActive] => 1
                        )
                )

        )

    [1] => stdClass Object
        (
            [customer] => xxxx
            [city] => Carver
            [state] => MA
            [zipcode] => 02330
            [totalUsers] => 3
            [totalActiveUsers] => 3
            [totalInActiveUsers] => 0
            [users] => Array
                (
                    [0] => stdClass Object
                        (
                            [firstName] => Leo
                            [lastName] => Furtado
                            [phoneNumber] => 781-000-0000
                            [userName] => LFurtado@xxx.com
                            [createdBy] => TConger@ccccc.com
                            [isActive] => 1
                        )                

                )

        )
)

Table 看起来像这样

您将必须为各个用户创建标记。创建用于构建外部行和个人用户详细信息的函数可能会让您的生活更轻松。

<?php
$token_result = <<<END
[
  {
    "customer": "SWSH",
    "city": "Thomasville",
    "state": "GA",
    "zipcode": "31792",
    "totalUsers": 6,
    "totalActiveUsers": 6,
    "totalInActiveUsers": 0,
    "users": [
      {
        "firstName": "xxx",
        "lastName": "xxx",
        "phoneNumber": "555-5555",
        "userName": "cb_igwrth@xxx.com",
        "createdBy": "someone",
        "isActive": 1
      },
      {
        "firstName": "Dan",
        "lastName": "Stewart",
        "phoneNumber": "+11111111111",
        "userName": "dan.sxx@xxx.ga.gov",
        "createdBy": "kwilliams@xxx.com",
        "isActive": 1
      }
    ]
  },
  {
    "customer": "xxxx",
    "city": "Carver",
    "state": "MA",
    "zipcode": "02330",
    "totalUsers": 3,
    "totalActiveUsers": 3,
    "totalInActiveUsers": 0,
    "users": [
      {
        "firstName": "Leo",
        "lastName": "Furtado",
        "phoneNumber": "781-000-0000",
        "userName": "LFurtado@xxx.com",
        "createdBy": "TConger@ccccc.com",
        "isActive": 1
      }
    ]
  }
]
END;



$someArray = json_decode($token_result);

function buildRowMarkupFromValue($value)
{
    $userDetails = implode('', array_map('buildUserDetailMarkupFromUser', $value->users));

    return <<<END
<tr>
    <td>$value->customer</td>
    <td>$value->city</td>
    <td>$value->state</td>
    <td>$value->zipcode</td>
    <td>$value->totalUsers</td>
    <td>$value->totalActiveUsers</td>
    <td>$value->totalInActiveUsers</td>
    <td>
        $userDetails
    </td>
</tr>

END;
}

function buildUserDetailMarkupFromUser($user)
{
    $activeIndicator = ($user->isActive) ? 'Active':'Inactive';

    return <<<END
$user->firstName $user->lastName (<a href="mailto:$user->userName">$user->userName</a>)<br>
<a href="tel:$user->phoneNumber">$user->phoneNumber</a><br>
Created by <a href="mailto:$user->createdBy">$user->createdBy</a><br>
$activeIndicator<br>
<br>
END;

}

$combined = array_map('buildRowMarkupFromValue', $someArray);

foreach ($combined as $value) {
    print $value;
}

最终最好想出一种分离模板和代码的方法,这很丑陋。