Php Codeception-REST-从数组返回第x个键
我试图从Codeception通过其REST模块(更具体地说是“grabDataFromJsonResponse”方法)回调的数组中获取密钥。我想从该数组中提取第一个键,因为grabDataFromJsonResponse函数允许我选择足够远的值,以便只返回所需的数据。然而,Codeception似乎将其转换为对象,因此,我得到了错误的密钥。下面是codesample,以及Codeception返回的数组对象的示例(顶部):Php Codeception-REST-从数组返回第x个键,php,arrays,object,codeception,Php,Arrays,Object,Codeception,我试图从Codeception通过其REST模块(更具体地说是“grabDataFromJsonResponse”方法)回调的数组中获取密钥。我想从该数组中提取第一个键,因为grabDataFromJsonResponse函数允许我选择足够远的值,以便只返回所需的数据。然而,Codeception似乎将其转换为对象,因此,我得到了错误的密钥。下面是codesample,以及Codeception返回的数组对象的示例(顶部): public function returningArrayKey(
public function returningArrayKey(WebGuy $I)
{
$I->sendPOST(mypostdata);
$I->seeResponseCodeIs(200);
$I->seeResponseContains("Success");
$jsonListingObj = $I->grabDataFromJsonResponse("tree.traversing.traversed");
$I->checkAgainstKey("123456789", key($jsonListingObj));
}
函数checkAgainstKey只执行AssertEquals:
function compareListingId($listingId, $oJsonObjectData)
{
$this->assertEquals($listingId, $oJsonObjectData);
}
但是,assertEquals将始终失败,因为第一个键如下所示:
Codeception\Maybe Object
(
[position:protected] => 0
[val:protected] => Array
(
[123456] => Array
( etc.
如上所述使用key()返回“position:protected”。我如何挖掘阵列并返回123456?123456表示的数组键将基于REST响应是动态的
谢谢 最终的解决方案是将对象强制转换为数组,切片数组(因为Codeception转换为数组的对象可能会添加公共属性,因此我们希望剥离),然后提取所需的密钥:
$jsonListingObj = $I->grabDataFromJsonResponse("tree.traversing.traversed");
$jsonListingArray = (array)$jsonListingObj;
$JSONParsed = key(current(array_slice($jsonListingArray, 1,1)));
$JSONParsed然后在上面的示例中返回“123456”