Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(PHP) Generic code to get different structure of arrays (objects)

Tags:

arrays

php

I have 2 structures of arrays (objects) returned :

1) If only 1 data 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] => [email protected]
                                    [2] => FIRST_NAME_001
                                    [3] => LAST_NAME_001
                                    [4] => M
                                    [5] => 01/01/1991
                                )
                        )**

                )

        )

)

2) If more than 1 data 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] => [email protected]
                                            [2] => FIRST_NAME_001
                                            [3] => LAST_NAME_001
                                            [4] => M
                                            [5] => 01/01/1991
                                        )
                                )
                            [1] => stdClass Object
                                (
                                    [fieldValues] => Array
                                        (
                                            [0] => CUSTOMER_ID_002
                                            [1] => [email protected]
                                            [2] => FIRST_NAME_002
                                            [3] => LAST_NAME_002
                                            [4] => F
                                            [5] => 02/02/1992
                                        )
                                )
                        )**

                )

        )

)

Currently i am 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 does not work when only 1 data returned.

How can I modify my code to get both structures?

Thank You.

like image 458
130nrd Avatar asked Feb 14 '26 16:02

130nrd


1 Answers

I believe this should do the trick:

// If it is already an array, leave it as an array, if it's 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>";
}
like image 198
dsojevic Avatar answered Feb 16 '26 10:02

dsojevic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!