phpobjectebay-apiarrayaccessebay-sdk

Accessing object element by index


I'm using eBay API which returns:

DTS\eBaySDK\Types\RepeatableType Object
(
    [data:DTS\eBaySDK\Types\RepeatableType:private] => Array
        (
            [0] => 60
        )

    [position:DTS\eBaySDK\Types\RepeatableType:private] => 0
    [class:DTS\eBaySDK\Types\RepeatableType:private] => DTS\eBaySDK\Shopping\Types\NameValueListType
    [property:DTS\eBaySDK\Types\RepeatableType:private] => Value
    [expectedType:DTS\eBaySDK\Types\RepeatableType:private] => string
)

... and if I try to access it like so:

echo $ItemSpecific->Value->{0};

... I get this error:

Notice: Undefined property: DTS\eBaySDK\Types\RepeatableType::$0 in ...


Solution

  • Since DTS\eBaySDK\Types\RepeatableType implements ArrayAccess interface, you can access the items of the private $data array as follows:

    foreach ($ItemSpecific as $item) {
      var_dump($item);
    }
    

    or by index: $ItemSpecific[0]. But it's more likely that you need to iterate the object in one way or another.