首页 > 解决方案 > 将对象转换为普通的多维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);
}

但它只是给出一个空数组:(。

预先感谢您的帮助。

标签: phparraysobject

解决方案


推荐阅读