0

I have an api call which returns data

stdClass Object
(
    [data] => stdClass Object
        (
            [TransactionId] => 10254
            [RequestId] => 1548
            [ResponseTime] => 0.161
            [SSP] => test1542
            [ErrorCode] => 0
            [NREC] => 1
            [BILREC] => 1
            [TC] => 0
            [USID] => BQ10
            [ASInput] => stdClass Object
                (
                    [User] => test1254
                    [HostIP] => 124.1.1.1
                    [CS] => test
                    [RId] => 16546asdfs
                    [DPPAPurpose] => 2
                    [GLBPurpose] => 3
                    [PermissibleUseCode] => 3
                    [NumberOfRecords] => 100
                    [StartingRecord] => 1
                    [Version] => 32
                    [Address] => stdClass Object
                        (
                            [Line1] => 125 main st
                            [City] => BROOKLYN
                            [State] => NY
                            [Zip] => 11237
                        )

                )
        )
)

To prevent the multiple call to api for testing how can I convert this to regular php array object so that i can use this output coming in this format to a variable.


    $string = "stdClass Object
    (
        [data] => stdClass Object
            (
                [TransactionId] => 10254
                [RequestId] => 1548
                [ResponseTime] => 0.161
                [SSP] => test1542
                [ErrorCode] => 0
                [NREC] => 1
                [BILREC] => 1
                [TC] => 0
                [USID] => BQ10
                [ASInput] => stdClass Object
                    (
                        [User] => test1254
                        [HostIP] => 124.1.1.1
                        [CS] => test
                        [RId] => 16546asdfs
                        [DPPAPurpose] => 2
                        [GLBPurpose] => 3
                        [PermissibleUseCode] => 3
                        [NumberOfRecords] => 100
                        [StartingRecord] => 1
                        [Version] => 32
                        [Address] => stdClass Object
                            (
                                [Line1] => 125 main st
                                [City] => BROOKLYN
                                [State] => NY
                                [Zip] => 11237
                            )

                    )
            )
    )";
    $array_obj = somefunction($string);

Something like that.

11
  • Leave it as an object, why convert it, its perfectly usable like this Commented Aug 13, 2015 at 19:52
  • I have no idea what you want to do? Commented Aug 13, 2015 at 19:52
  • Do you mean you want to serialize it and save it to a local file so you can re-hydrate it without calling the api again? Commented Aug 13, 2015 at 19:53
  • You could iterate it, passing each value to an array and force (string) casting, but I don't see the point in doing that. Can you provide the code that generates this output? Commented Aug 13, 2015 at 19:53
  • 1
    when i run the print_r($api_return) that the output i get. Now i am trying to create the proper format for html. But while testing i have to call the api over and over for the output. so if i can convert above data to php array that will save my api credits Commented Aug 13, 2015 at 20:01

4 Answers 4

1

I've tried to simulate your object data to understand your issue.

Class ApiObj {

    public $data;

}

Class ApiObjData {

    public $TransactionId = 10254;
    public $RequestId = 1548;
    public $ResponseTime = 0.161;
    public $SSP = 'test1542';
    public $ErrorCode = 0;
    public $NREC = 1;
    public $BILREC = 1;
    public $TC = 0;
    public $USID = 'BQ10';
    public $ASInput;

}

Class ApiObjDataASInput {

    public $User = 'test1254';
    public $HostIP = '124.1.1.1';
    public $CS = 'test';
    public $RId = '16546asdfs';
    public $DPPAPurpose = 2;
    public $GLBPurpose = 3;
    public $PermissibleUseCode = 3;
    public $NumberOfRecords = 100;
    public $StartingRecord = 1;
    public $Version = 32;
    public $Address;
}

Class ApiObjDataASInputAddress {

    public $Line1 = '125 main st';
    public $City = 'BROOKLYN';
    public $State = 'NY';
    public $Zip = 11237;
}

$api_response = new ApiObj;
$api_response->data = new ApiObjData;
$api_response->data->ASInput = new ApiObjDataASInput;
$api_response->data->ASInput->Address = new ApiObjDataASInputAddress;

echo "<pre>";
print_r($api_response);

And I've got similiar output:

ApiObj Object
(
    [data] => ApiObjData Object
        (
            [TransactionId] => 10254
            [RequestId] => 1548
            [ResponseTime] => 0.161
            [SSP] => test1542
            [ErrorCode] => 0
            [NREC] => 1
            [BILREC] => 1
            [TC] => 0
            [USID] => BQ10
            [ASInput] => ApiObjDataASInput Object
                (
                    [User] => test1254
                    [HostIP] => 124.1.1.1
                    [CS] => test
                    [RId] => 16546asdfs
                    [DPPAPurpose] => 2
                    [GLBPurpose] => 3
                    [PermissibleUseCode] => 3
                    [NumberOfRecords] => 100
                    [StartingRecord] => 1
                    [Version] => 32
                    [Address] => ApiObjDataASInputAddress Object
                        (
                            [Line1] => 125 main st
                            [City] => BROOKLYN
                            [State] => NY
                            [Zip] => 11237
                        )

                )

        )

)

As you can see, the data inside the objects can be accessed just as they would be inside an array, because object structure matches the one of an array. They have similar behavior.

You can parse it, iterate it, echo it, do whatever you want with it, regardless if it is an object or array.

That said, there is no reason for you to do that. Whatever is the problem you're facing, any answers to your question as it is presented won't solve it.

I suggest you analyze your issue better and ask again, or try to approach it in another context.

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

Comments

0

You can cast a stdobj to an array in PHP:

<?php
$data_array = (array) $obj->data;

2 Comments

The way I interpreted the question, the asker wanted the object in array format.
That's what it sounded like to me too, however, this will only convert the outer part. all of the sub-objects will still be objects.
0

I'm not totally sure, but I think you may be wanting to construct an object yourself like the one the API outputs, so that you can experiment on it instead of making repeated API calls. If that is the case, there are a couple of ways to do that. Here is one:

$object = new stdClass;
$object->data = new stdClass;
$object->data->TransactionId = 10254;
$object->data->RequestId = 1548;
$object->data->ResponseTime = 0.161;
$object->data->SSP = 'test1542';
$object->data->ErrorCode = 0;
$object->data->NREC = 1;
$object->data->BILREC = 1;
$object->data->TC = 0;
$object->data->USID = 'BQ10';
$object->data->ASInput = new stdClass;
$object->data->ASInput->User = 'test1254';
$object->data->ASInput->HostIP = '124.1.1.1';
$object->data->ASInput->CS = 'test';
$object->data->ASInput->RId = '16546asdfs';
$object->data->ASInput->DPPAPurpose = 2;
$object->data->ASInput->GLBPurpose = 3;
$object->data->ASInput->PermissibleUseCode = 3;
$object->data->ASInput->NumberOfRecords = 100;
$object->data->ASInput->StartingRecord = 1;
$object->data->ASInput->Version = 32;
$object->data->ASInput->Address = new stdClass;
$object->data->ASInput->Address->Line1 = '125 main st';
$object->data->ASInput->Address->City = 'BROOKLYN';
$object->data->ASInput->Address->State = 'NY';
$object->data->ASInput->Address->Zip = '11237';

Comments

0

I am assuming that the class you showed us is the result of an API call that returns a JSON String which you run through a json_decode();something like this.

$json_string = $api_get(....);
$obj = json_decode($json_string);

This creates the json string into an equivalent PHP data type, an Object in this case.

Now if you just change the json_decode(); and add the second param as true it will do the conversion of the json string to a PHP array regardless of the original data type of the json string.

So instead do

$json_string = $api_get(....);
$array = json_decode($json_string,true);

And you will get an array as a result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.