PHP Need nested array values to dump into html table cell

104 Views Asked by At

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] => [email protected]
                            [createdBy] => 
                            [isActive] => 1
                        )

                    [1] => stdClass Object
                        (
                            [firstName] => Dan
                            [lastName] => Stewart
                            [phoneNumber] => +11111111111
                            [userName] => [email protected]
                            [createdBy] => [email protected]
                            [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] => [email protected]
                            [createdBy] => [email protected]
                            [isActive] => 1
                        )                

                )

        )
)

Table Looks like this

enter image description here

1

There are 1 best solutions below

1
Rob Ruchte On BEST 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": "[email protected]",
        "createdBy": "someone",
        "isActive": 1
      },
      {
        "firstName": "Dan",
        "lastName": "Stewart",
        "phoneNumber": "+11111111111",
        "userName": "[email protected]",
        "createdBy": "[email protected]",
        "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": "[email protected]",
        "createdBy": "[email protected]",
        "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.