Skip to content
Advertisement

PHP Need nested array values to dump into html table cell

I have a stdClass Object array with nested User arrays as a value that I would like to display in a table cell.

I am struggling and have exhausted a lot of time. I am hoping to get a shove in the right direction if possible.

The image below will show what I am trying to do.

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

Here is what my Array and Objects look like

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 Looks like this

enter image description here

Advertisement

Answer

You’re going to have to create the markup for the individual users. It would probably make your life easier to create functions for building the outer rows and the individual user details.

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

Eventually it would be better to come up with a way of separating your templates and code, this is pretty ugly.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement