我有一个api调用,它返回数据
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 ) ) ) )
为了防止多次调用api进行测试,如何将其转换为常规php数组对象,以便我可以将此格式的输出用于变量。
$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);
类似的东西。
答案 0 :(得分:1)
我已尝试模拟您的对象数据以了解您的问题。
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);
我有类似的输出:
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
)
)
)
)
正如您所看到的,对象内部的数据可以像在数组中一样访问,因为对象结构匹配数组。他们有类似的行为。
您可以解析它,迭代它,回显它,随意做任何事情,无论它是一个对象还是数组。
那就是说,你没有理由这样做。无论您面临的问题是什么,您提出的问题的任何答案都无法解决。
我建议你更好地分析你的问题并再次提问,或尝试在另一个环境中接近它。
答案 1 :(得分:0)
您可以在PHP中将stdobj强制转换为数组:
<?php
$data_array = (array) $obj->data;
答案 2 :(得分:0)
我不完全确定,但我认为您可能想要自己构建一个像API输出的对象,以便您可以对其进行试验,而不是重复进行API调用。如果是这种情况,有几种方法可以做到这一点。这是一个:
$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';
答案 3 :(得分:0)
我假设您向我们展示的类是API调用的结果,该API调用返回一个JSON字符串,您通过这样的json_decode();
运行。
$json_string = $api_get(....);
$obj = json_decode($json_string);
这会将json字符串创建为等效的PHP数据类型,在本例中为Object。
现在,如果您只是更改json_decode();
并将第二个参数添加为true
,则无论json字符串的原始数据类型如何,它都会将json字符串转换为PHP数组。 / p>
所以改为
$json_string = $api_get(....);
$array = json_decode($json_string,true);
结果你会得到一个数组。