Jump to content

PHP Need nested user array values to dump into html table cell


Thomas_L
 Share

Recommended Posts

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 is attached

 

sampletable.JPG

Link to comment
Share on other sites

I truly would like to loop through the user array in the current foreach loop and dump all of the values into the cell for now. After i get that working my end result will be a link that pops up a Bootstrap Modal that contains all of the user array data for each customer.

 

secondsample.JPG

Edited by Thomas_L
Link to comment
Share on other sites

[{"customer":"SWSH","city":"Thomasville","state":"GA","zipcode":"31792","totalUsers":6,"totalActiveUsers":6,"totalInActiveUsers":0,"users":[{"firstName":"xxxx","lastName":"Brooks","phoneNumber":"","userName":"cb_igwrth@xxx.com","createdBy":null,"isActive":true},{"firstName":"Dan","lastName":"Stewart","phoneNumber":"+11111111111","userName":"dan.stxxt@xxxx.ga.gov","createdBy":"kwilliams@xxx.com","isActive":true},{"firstName":"Bill","lastName":"Mott","phoneNumber":"(229) 000-1231","userName":"bill@bxxxr.net","createdBy":null,"isActive":true},{"firstName":"SWSH","lastName":"Test","phoneNumber":"(229) 000-3024","userName":"swsh.test@me.com","createdBy":null,"isActive":true},{"firstName":"Cleaver","lastName":"Brooks","phoneNumber":"","userName":"cb_illngwth@xxxx.com","createdBy":null,"isActive":true},{"firstName":"Fred","lastName":"Baker","phoneNumber":"1111111111","userName":"fred.baker@xxxx.gov","createdBy":null,"isActive":true}]}

As requested the Json Format

Thanks for all of the help

Edited by Thomas_L
Link to comment
Share on other sites

1 hour ago, dodgeitorelse3 said:

missing closing square bracket at end of Json Format

That's what I added, but processing it now gives $value as an array and not as an object as it was originally being processed.

I get the feeling a chunk has just  been copied out of the original value (as there is only a single customer) and then pasted here.

I suggest that @Thomas_L writes a function to process $value->users and return whatever he wants in that column, thus...

        '</td><td>
           <a href="">' .
             getUserData($value->users) .
        "</a></td></tr>";

Example

function getUserData($udata)
{
    $res = '';
    foreach ($udata as $u) {
        $res .= "Name : {$u['firstName']} {$u['lastName']} <br>";
    }
    return $res;
}  

 

Edited by Barand
Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.