1

I'm using an API to get data from a company. The array being returned is shown below (DATA BEING RETURNED). I could get up to 10 responses.

I'm trying to extract values by using a foreach loop shown below (FOREACH LOOP).

I want to loop through and increment the value of [0] so I can display all 10 responses on the screen instead of copying and pasting that code 10 times.

I've tried a for statement to increment $x and usually get Illegal string offset errors.

Help appreciated!

Output of var_dump:

array(3) { ["ResponseCode"]=> int(200) ["ResponseText"]=> string(2) "OK" ["Data"]=> array(1) { [0]=> array(9) { ["LastName"]=> string(6) "Brewer" ["cobalt_IARRELicenseNumber"]=> NULL ["ContactId"]=> string(36) "xxxxxxxx-f0a2-4ca6-8ab1-693bb3fd8ad2" ["cobalt_NRDSID"]=> string(9) "xxxxxxx73" ["EMailAddress1"]=> string(15) "mbxxxx@xxxr.org" ["FirstName"]=> string(4) "Mxxx" ["Telephone1"]=> string(12) "217-529-2xx0" ["cobalt_Username"]=> string(7) "mxxxx" ["cobalt_contact_cobalt_membership"]=> array(1) { [0]=> array(3) { ["cobalt_MemberTypeId"]=> array(3) { ["Type"]=> string(34) "EntityReference: cobalt_membertype" ["Value"]=> string(36) "xxxxxxb0-ef20-e111-b470-00155d000140" ["Display"]=> string(5) "Staff" } ["cobalt_OfficeId"]=> array(3) { ["Type"]=> string(24) "EntityReference: account" ["Value"]=> string(36) "cxxxxxe-0cc1-449b-af79-6b9e85464c7c" ["Display"]=> string(27) "xxx Asxxxxxx" } ["statuscode"]=> array(3) { ["Type"]=> string(9) "OptionSet" ["Value"]=> int(1) ["Display"]=> string(6) "Active" } } } } } }

FOREACH LOOP:

foreach($data as $value){
if (isset($value[0]['LastName'])){
        echo '<b>Last Name: </b>';
        echo $value[0]['LastName'] .' <br>';

        echo '<b>First Name: </b>';
        echo $value[0]['FirstName'] .' <br>';

        echo '<b>NRDS ID: </b>';
        echo $value[0]['cobalt_NRDSID'] .' <br>';

        echo '<b>Member Type: </b>';
        echo $value[0]['cobalt_contact_cobalt_memberships'][0]            ['cobalt_MemberTypeId']['Display'] .' <br>';

        echo '<b>Status: </b>';
        echo $value[0]['cobalt_contact_cobalt_memberships'][0]['statuscode']['Display'];

}

}

DATA BEING RETURNED:
Array
(
[ResponseCode] => 200
[ResponseText] => OK
[Data] => Array
    (
        [0] => Array
            (
                [LastName] => FLACK
                [ContactId] => d4edfefc-xxxx-xxxx-xxxx-xxxx51608a9c
                [cobalt_NRDSID] => 37xxxx119
                [FirstName] => JANE
                [cobalt_contact_cobalt_memberships] => Array
                    (
                        [0] => Array
                            (
                                [cobalt_MemberTypeId] => Array
                                    (
                                        [Type] => EntityReference: cobalt_membertype
                                        [Value] => dxxxx4b0-ef20-e111-b470-00155d000140
                                        [Display] => MEMBER
                                    )

                                [cobalt_OfficeId] => Array
                                    (
                                        [Type] => EntityReference: account
                                        [Value] => xxxxxxxx-b4b7-4b87-90fa-5e644e8ce516
                                        [Display] => BANK
                                    )

                                [statuscode] => Array
                                    (
                                        [Type] => OptionSet
                                        [Value] => 2
                                        [Display] => Inactive
                                    )

                            )

                    )

            )

       Array
(

    (
        [1] => Array
            (
                [LastName] => DOE
                [ContactId] => d4edfefc-xxxx-xxxx-xxxx-xxxx51608a9c
                [cobalt_NRDSID] => 37xxxx120
                [FirstName] => JANE
                [cobalt_contact_cobalt_memberships] => Array
                    (
                        [0] => Array
                            (
                                [cobalt_MemberTypeId] => Array
                                    (
                                        [Type] => EntityReference: cobalt_membertype
                                        [Value] => dxxxx4b0-ef20-e111-b470-00155d000140
                                        [Display] => MEMBER
                                    )

                                [cobalt_OfficeId] => Array
                                    (
                                        [Type] => EntityReference: account
                                        [Value] => xxxxxxxx-b4b7-4b87-90fa-5e644e8ce516
                                        [Display] => BANK
                                    )

                                [statuscode] => Array
                                    (
                                        [Type] => OptionSet
                                        [Value] => 2
                                        [Display] => Inactive
                                    )

                            )

                    )

            )

1 Answers1

0

What You have in DATA BEING RETURNED is the output of PHP print_r() function. You can try to recreate original data. Here are some topics already solved that:

first

second

Community
  • 1
  • 1
Valentas
  • 2,135
  • 1
  • 17
  • 21
  • Here's the output of var_dump: – Matt Brewer Mar 12 '15 at 16:19
  • As I mentioned, in Your `DATA BEING RETURNED`, the output is from PHP's `print_r()`. You can check difference here: http://stackoverflow.com/questions/3406171/php-var-dump-vs-print-r – Valentas Mar 16 '15 at 07:52