这是一个来自Stripe支付网关的响应数组:
$response = Array ( [deleteCardAccount] => Stripe\Card Object ( [_opts:protected] => Stripe\Util\RequestOptions Object ( [headers] => Array ( ) [apiKey] => sk_test_erwppHN9ibdfgdfg0CesaOwnDy ) [_values:protected] => Array ( [id] => card_18SwerIsEZ1YjoOMVAFRLA [currency] => usd [deleted] => 1 ) [_unsavedValues:protected] => Stripe\Util\Set Object ( [_elts:Stripe\Util\Set:private] => Array ( ) ) ...
上面的数组包含对象“Stripe \ Card Object”,我想得到元素[_values:protected]的值,这是一个数组。
当我将对象转换为数组时
$nasty_array = (array)$response['deleteCardAccount'];
print_r($nasty_array);
我得到的数组包含以星号开头的键:
Array ( [*_opts] => Stripe\Util\RequestOptions Object ( [headers] => Array ( ) [apiKey] => sk_test_wejYbwerCerwesaOefg ) [*_values] => Array ( [id] => card_18SwerIsEZ1YjoOMVAFRLA [currency] => usd [deleted] => 1 ) [*_unsavedValues] => Stripe\Util\Set Object ( [_elts:Stripe\Util\Set:private] => Array ( ) ) ...
然而,当我尝试
时print_r($nasty_array[*_opts]);
print_r($nasty_array[*_values]);
我得到了
error: Undefined index *_values
error: Undefined index *_opts
问题:
注意:当我手动创建一个以星号开头的键的数组时,我可以毫无问题地访问这些键,但由于某种原因,在将opject转换为数组时我无法访问类似的键;
答案 0 :(得分:0)
可以将数组中的对象捕获到变量中而不进行强制转换;
$captured_object = $response['deleteCardAccount'];
然后可以使用" arrow"简单地引用捕获对象的元素。符号:
print_r($captured_object->id);
print_r($captured_object->currency);
print_r($captured_object->deleted);
或者更简单的是,可以通过混合数组符号和箭头符号来直接引用对象:
print $response['deleteCardAccount']->id;
print $response['deleteCardAccount']->currency
print $response['deleteCardAccount']->deleted;
此方法适用于Stripe响应对象,但尚未使用其他具有受保护属性的对象进行测试。