0

Getting this type of response from the eBay API:

DTS\eBaySDK\Trading\Types\GetCategoriesResponseType Object
(
    [values:DTS\eBaySDK\Types\BaseType:private] => Array
        (
            [Timestamp] => DateTime Object
                (
                    [date] => 2019-12-07 22:25:23.938000
                    [timezone_type] => 2
                    [timezone] => Z
                )

            [Ack] => Success
            [Version] => 1119
            [Build] => E1119_CORE_APICATALOG_19043596_R1
            [CategoryArray] => DTS\eBaySDK\Trading\Types\CategoryArrayType Object

using this function to recursively create an array out of the objects.

function object_to_array($obj) {
    if(is_object($obj) || is_array($obj)) {
        $ret = (array) $obj;
        foreach($ret as &$item) {
            $item = object_to_array($item);
        }
        return $ret;
    }
    else {
        return $obj;
    }
}

This works and output is this with a print_r

Array
(
    [DTS\eBaySDK\Types\BaseTypevalues] => Array
        (
            [Timestamp] => Array
                (
                    [date] => 2019-12-07 22:27:15.925000
                    [timezone_type] => 2
                    [timezone] => Z
                )

            [Ack] => Success
            [Version] => 1119
            [Build] => E1119_CORE_APICATALOG_19043596_R1
            [CategoryArray] => Array
                (
                    [DTS\eBaySDK\Types\BaseTypevalues] => Array

But then I did a var_dump on the array and noticed this:

array (size=2)
  '�DTS\eBaySDK\Types\BaseType�values' => 
    array (size=5)
      'Timestamp' => 
        array (size=3)
          'date' => string '2019-12-07 22:23:51.847000' (length=26)
          'timezone_type' => int 2
          'timezone' => string 'Z' (length=1)
      'Ack' => string 'Success' (length=7)
      'Version' => string '1119' (length=4)
      'Build' => string 'E1119_CORE_APICATALOG_19043596_R1' (length=33)
      'CategoryArray' => 
        array (size=2)
          '�DTS\eBaySDK\Types\BaseType�values' => 

I've tried just copying and pasting-- $responsearray['�DTS\eBaySDK\Types\BaseType�values']

But just get the error

Notice: Undefined index: �DTS\eBaySDK\Types\BaseType�values

How can I work with these keys and access these array elements?

Brian Bruman
  • 883
  • 12
  • 28

1 Answers1

0

Not sure if best option, but fixed it with this

function transformKeys(&$array)
{
    foreach (array_keys($array) as $key):
        # Working with references here to avoid copying the value,
        # since you said your data is quite large.
        $value = &$array[$key];
        unset($array[$key]);
        # This is what you actually want to do with your keys:
        #  - remove exclamation marks at the front
        #  - camelCase to snake_case
        $transformedKey = preg_replace('/[\x00-\x1F\x7F-\xFF]/', '', $key);
        # Work recursively
        if (is_array($value)) transformKeys($value);
        # Store with new key
        $array[$transformedKey] = $value;
        # Do not forget to unset references!
        unset($value);
    endforeach;
    return $array;
}

Note preg_replace('/[\x00-\x1F\x7F-\xFF]/', '', $key); removes the invalid character.

Brian Bruman
  • 883
  • 12
  • 28