(PHP) Generic code for getting different structure of arrays (objects)

I have 2 structures of arrays (objects):

1) If only 1 data was returned :

stdClass Object
(
    [result] => stdClass Object
        (
            [recordData] => stdClass Object
                (
                    [fieldNames] => Array
                        (
                            [0] => CUSTOMER_ID_
                            [1] => EMAIL_ADDRESS_
                            [2] => FIRST_NAME
                            [3] => LAST_NAME
                            [4] => SEX
                            [5] => DATE_OF_BIRTH
                        )

                    **[records] => stdClass Object
                        (
                            [fieldValues] => Array
                                (
                                    [0] => CUSTOMER_ID_001
                                    [1] => email001@test.com
                                    [2] => FIRST_NAME_001
                                    [3] => LAST_NAME_001
                                    [4] => M
                                    [5] => 01/01/1991
                                )
                        )**

                )

        )

)

      

2) If more than 1 data is returned :

stdClass Object
(
    [result] => stdClass Object
        (
            [recordData] => stdClass Object
                (
                    [fieldNames] => Array
                        (
                            [0] => CUSTOMER_ID_
                            [1] => EMAIL_ADDRESS_
                            [2] => FIRST_NAME
                            [3] => LAST_NAME
                            [4] => SEX
                            [5] => DATE_OF_BIRTH
                        )

                    **[records] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [fieldValues] => Array
                                        (
                                            [0] => CUSTOMER_ID_001
                                            [1] => email001@test.com
                                            [2] => FIRST_NAME_001
                                            [3] => LAST_NAME_001
                                            [4] => M
                                            [5] => 01/01/1991
                                        )
                                )
                            [1] => stdClass Object
                                (
                                    [fieldValues] => Array
                                        (
                                            [0] => CUSTOMER_ID_002
                                            [1] => email002@test.com
                                            [2] => FIRST_NAME_002
                                            [3] => LAST_NAME_002
                                            [4] => F
                                            [5] => 02/02/1992
                                        )
                                )
                        )**

                )

        )

)

      

I am currently using this code:

foreach ($memberlist->result->recordData->records as $list)
    {   
        $out .= "<tr>";

        foreach ($list as $memberarray)
            {
                foreach ($memberarray as $member)
                    {
                        $out .= "<td>" . $member . "</td>";
                    }
            }

        $out .= "</tr>";
    }

      

but it doesn't work when only 1 data is returned.

How can I change my code to get both structures?

Thank.

+3


source to share


1 answer


I believe this should do the trick:



// If it is already an array, leave it as an array, if it not, place it in an array
$member_records = ( is_array( $memberlist->result->recordData->records ) ? $memberlist->result->recordData->records : array( $memberlist->result->recordData->records ) );

foreach ($member_records as $list)
{   
    $out .= "<tr>";

    foreach ($list as $memberarray)
        {
            foreach ($memberarray as $member)
                {
                    $out .= "<td>" . $member . "</td>";
                }
        }

    $out .= "</tr>";
}

      

+3


source







All Articles