0

I have an array which looks like this:

    Array
(
    [response] => Array
        (
            [dataInfo] => Array
                (
                    [totalRecordCount] => 362
                    [foundCount] => 5
                    [returnedCount] => 5
                )

            [data] => Array
                (
                    [0] => Array
                        (
                            [fieldData] => Array
                                (
                                    [groupAssetID] => 1020
                                    [groupAssetName] => Standard Equipment
                                )

                            [portalData] => Array
                                (
                                )

                            [recordId] => 823
                            [modId] => 1
                        )

                    [1] => Array
                        (
                            [fieldData] => Array
                                (
                                    [groupAssetID] => 1001
                                    [groupAssetName] => Tools
                                )

                            [portalData] => Array
                                (
                                )

                            [recordId] => 829
                            [modId] => 1
                        )

                    [2] => Array
                        (
                            [fieldData] => Array
                                (
                                    [groupAssetID] => 1005
                                    [groupAssetName] => Spare Parts
                                )

                            [portalData] => Array
                                (
                                )

                            [recordId] => 830
                            [modId] => 1
                        )

                )

        )

    [messages] => Array
        (
            [0] => Array
                (
                    [code] => 0
                    [message] => OK
                )

        )

)

I'm trying to loop over this and extract the values for groupAssetID and groupAssetName in the data array but haven't been able to get this to work so far. I've tried:

    foreach ( $records as $record) {
        echo $field . ": " . $value . "<br />\r\n" ;
    }

and

     foreach ( $records->fieldData as $field=>$value) {
        echo $field . ": " . $value . "<br />\r\n" ;
    }

but none of these seem to work and I can't work out the correct syntax here.

1

1 Answer 1

0

I managed it to run with this syntax:

$records = array
(
    'response' => array
        (
            'dataInfo' => array
                (
                    'totalRecordCount' => 362,
                    'foundCount' => 5,
                    'returnedCount' => 5
                ),

            'data' => array
                (
                    0 => array
                        (
                            'fieldData' => array
                                (
                                    'groupAssetID' => 1020,
                                    'groupAssetName' => 'Standard Equipment'
                                ),

                            'portalData' => array
                                (
                                ),

                            'recordId' => 823,
                            'modId' => 1
                        ),

                    1 => array
                        (
                            'fieldData' => array
                                (
                                    'groupAssetID' => 1001,
                                    'groupAssetName' => 'Tools'
                                ),

                            'portalData' => array
                                (
                                ),

                            'recordId' => 829,
                            'modId' => 1
                        ),

                    2 => array
                        (
                            'fieldData' => array
                                (
                                    'groupAssetID' => 1005,
                                    'groupAssetName' => 'Spare Parts'
                                ),

                            'portalData' => array
                                (
                                ),

                            'recordId' => 830,
                            'modId' => 1
                        )
                )
        ),

    'messages' => array
        (
            0 => array
                (
                    'code' => 0,
                    'message' => 'OK'
                )
        )
);

foreach ($records['response']['data'] as $key => $value) {
        
    echo $value['fieldData']['groupAssetID'] . ": " . $value['fieldData']['groupAssetName'] . "<br />\r\n" ;
}

Output:

1020: Standard Equipment
1001: Tools
1005: Spare Parts

Is this what you need?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.