Search code examples
phpjsonassociative-arraybox-api

Converting an nested xAPI JSON output into PHP Array


Array
(
    [0] => TinCan\Statement Object
        (
            [id:protected] => 0a53e06c-64a7-4902-930e-993bb228cd49
            [stored:protected] => 2018-02-24T04:21:22.456Z
            [authority:protected] => TinCan\Agent Object
                (
                    [objectType:protected] => Agent
                    [name:protected] => sabarish
                    [mbox:protected] => 
                    [mbox_sha1sum:protected] => 
                    [openid:protected] => 
                    [account:protected] => TinCan\AgentAccount Object
                        (
                            [name:protected] => 248a06f20aa62f
                            [homePage:protected] => https://sandbox.watershedlrs.com
                        )
            )

        [version:protected] => 1.0.0
        [attachments:protected] => Array
            (
            )

        [actor:protected] => TinCan\Agent Object
            (
                [objectType:protected] => Agent
                [name:protected] => Akshaya Manikandan
                [mbox:protected] => mailto:[email protected]
                [mbox_sha1sum:protected] => 
                [openid:protected] => 
                [account:protected] => 
            )

        [verb:protected] => TinCan\Verb Object
            (
                [id:protected] => http://adlnet.gov/expapi/verbs/skipped
                [display:protected] => TinCan\LanguageMap Object
                    (
                        [_map:protected] => Array
                            (
                                [en] => skipped
                            )

                    )

            )

        [target:protected] => TinCan\Activity Object
            (
                [objectType:TinCan\Activity:private] => Activity
                [id:protected] => https://app.acuizen.com/populate_form/965/1573/4690
                [definition:protected] => 
            )

        [result:protected] => 
        [context:protected] => TinCan\Context Object
            (
                [registration:protected] => 
                [instructor:protected] => 
                [team:protected] => 
                [contextActivities:protected] => TinCan\ContextActivities Object
                    (
                        [category:protected] => Array
                            (
                                [0] => TinCan\Activity Object
                                    (
                                        [objectType:TinCan\Activity:private] => Activity
                                        [id:protected] => http://acuizen.com/ActivitySkipped
                                        [definition:protected] => TinCan\ActivityDefinition Object
                                            (
                                                [type:protected] => http://id.tincanapi.com/activitytype/Assignment
                                                [name:protected] => TinCan\LanguageMap Object
                                                    (
                                                        [_map:protected] => Array
                                                            (
                                                            )

                                                    )

                                                [description:protected] => TinCan\LanguageMap Object
                                                    (
                                                        [_map:protected] => Array
                                                            (
                                                            )

                                                    )

                                                [moreInfo:protected] => 
                                                [extensions:protected] => TinCan\Extensions Object
                                                    (
                                                        [_map:protected] => Array
                                                            (
                                                            )

                                                    )

                                                [interactionType:protected] => 
                                                [correctResponsesPattern:protected] => 
                                                [choices:protected] => 
                                                [scale:protected] => 
                                                [source:protected] => 
                                                [target:protected] => 
                                                [steps:protected] => 
                                            )

                                    )

                            )

                        [parent:protected] => Array
                            (
                            )

                        [grouping:protected] => Array
                            (
                            )

                        [other:protected] => Array
                            (
                            )

                    )

                [revision:protected] => 
                [platform:protected] => 
                [language:protected] => 
                [statement:protected] => 
                [extensions:protected] => TinCan\Extensions Object
                    (
                        [_map:protected] => Array
                            (
                            )

                    )

            )

        [timestamp:protected] => 2018-02-24T04:21:22.456Z
    )

... + 100 more like these.

I get this output after running my php code to retrive all information from the LRS. How to change this into PHP ARRAY?


Solution

  • You need to learn how to use their API documentation, There is no simple "get all the data" that I can see because everything is wrapped in a class with private/protected properties....

    Using your provided sample, here is an example of how to "get the actors"

    $Statements = getAllActivity()->content->getStatements();
    
    foreach( $Statements as $Statement )
    {
      print_r( $Statement->getActor() );
      print_r( $Statement->getActor()->getName() );
      print_r( $Statement->getActor()->getMbox() );
    }
    

    Review the script \src\StatementBase.php, this is where I found the getAcator() method