php - 将对象转换为普通的多维php数组
问题描述
我有一个 php 变量$response
,其中包含一个带有受保护键的对象:
FindCompletedItemsResponse Object (
[searchResult:protected] => SearchResult Object (
[item:protected] => Array (
[0] => SearchItem Object (
[itemId:protected] => 383198481839
[title:protected] => Matchbox SCX SRS2 Jaguar "XJR-14" Super Racing Castrol #4 Slot Car 1/32 NIB
[globalId:protected] => EBAY-US
[primaryCategory:protected] => Category Object (
[categoryId:protected] => 4781
[categoryName:protected] => 1970-Now
)
[galleryURL:protected] => https://thumbs4.ebaystatic.com/m/mslyccjMA3BvwuDua4N-kkg/140.jpg
[viewItemURL:protected] => https://www.ebay.com/itm/Matchbox-SCX-SRS2-Jaguar-XJR-14-Super-Racing-Castrol-4-Slot-Car-1-32-NIB-/383198481839
[paymentMethod:protected] => Array ( [0] => PayPal )
[postalCode:protected] => 923**
[location:protected] => Fontana,CA,USA
[country:protected] => US
[shippingInfo:protected] => ShippingInfo Object (
[shippingServiceCost:protected] => Amount Object (
[attributeValues] => Array (
[currencyId] => USD
)
[value:protected] => 8.95
)
[shippingType:protected] => Flat
[shipToLocations:protected] => Array (
[0] => Worldwide
)
[expeditedShipping:protected] => 1
[handlingTime:protected] => 1
)
[sellingStatus:protected] => SellingStatus Object (
[currentPrice:protected] => Amount Object (
[attributeValues] => Array (
[currencyId] => USD
)
[value:protected] => 49.95
)
[convertedCurrentPrice:protected] => Amount Object (
[attributeValues] => Array (
[currencyId] => USD
)
[value:protected] => 49.95
)
[sellingState:protected] => EndedWithSales
)
[listingInfo:protected] => ListingInfo Object (
[startTime:protected] => 2019-10-07T18:28:15.000Z
[endTime:protected] => 2020-01-15T22:19:56.000Z
[listingType:protected] => StoreInventory
)
[returnsAccepted:protected] => 1
[condition:protected] => Condition Object (
[conditionId:protected] => 1000
[conditionDisplayName:protected] => New
)
)
)
[attributeValues] => Array (
[count] => 1
)
)
[paginationOutput:protected] => PaginationOutput Object (
[pageNumber:protected] => 1
[entriesPerPage:protected] => 100
[totalPages:protected] => 1
[totalEntries:protected] => 1
)
[ack:protected] => Success
[version:protected] => 1.13.0
[timestamp:protected] => 2020-04-09T10:01:15.148Z
)
我想将它转换为一个普通的多维 php 数组,我可以在其中访问例如标题,如$response[0]["title"]
.
我试过了get_object_vars($response)
,json_decode(json_encode($response), true)
但是当我查看打印结果时,这并没有做任何事情。
我尝试了这个功能:
function o2a($obj) {
if(!is_array($obj) && !is_object($obj)) return $obj;
if(is_object($obj)) $obj = get_object_vars($obj);
return array_map(__FUNCTION__, $obj);
}
但它只是给出一个空数组:(。
预先感谢您的帮助。
解决方案
推荐阅读
- ios - 前提条件失败:属性设置初始值失败:78 | iOS 13.2.2 | XCode 11.2.1
- c - 防止头文件双重包含的不同版本,哪个是正确的?
- python - 在 JPG 文件中嵌入数据而不更改 python 中的图像
- python - Python中的递归前序二叉树遍历 - 无类型错误
- sql - 在单个查询中获取分页行和总数
- command-line - 为什么 GREP end of line 不适用于此文件?
- module - Tricentis Tosca - 添加 TestStep 模块不包括模块的控件?
- c# - 如果目前仅提供部分内容,如何使用 c# 获取 HTTPWebRequest 的完整 json 内容?
- c - 无法将所有行 fscanf 到 Struct 数组中
- r - R,加回拟合值 plm(),拟合值小于回归中的观测值