我正在尝试迭代存储在Stripe customer对象中的元数据。
我能数出物品的数量:
echo count($matchUser->data[0]->metadata);这给了我预期的'2‘。但是:
foreach($matchUser->data[0]->metadata as $key => $value) {
echo $key;
echo $value;
echo "hello";
}什么都不回。
元数据的Var转储如下:
object(Stripe\StripeObject)#67 (2) { ["testitem"]=> string(5) "hello" ["password_hash"]=> string(6) "myhash" }发布于 2018-12-27 10:48:03
试试看
public function __toArray($recursive = false)
{
if ($recursive) {
return Util\Util::convertStripeObjectToArray($this->_values);
} else {
return $this->_values;
}
}就像这样:
$matchArray = $matchUser->__toArray();要更深入地了解可用的方法,请查看这个url:
https://github.com/stripe/stripe-php/blob/master/lib/StripeObject.php
希望它能帮上忙
发布于 2019-04-05 12:36:37
我知道这个问题已有几个月的历史,而且已经有了一个公认的答案。不过,我认为有一个更好的方法。
我认为用于Stripe的PHP库的开发人员使用__ (双下划线)前缀一些方法来指示该方法是受保护的还是私有的,这是一个古老的惯例,因为在PHP中,方法的可见性是不存在的。现在,__前缀保留给魔术方法,如PHP:魔术方法-手册中所述。
PHP保留以__开头的所有函数名,都是神奇的。
在深入了解班级之后,我认为定义如下的jsonSerialize方法是一个更好的选择。
public function jsonSerialize()
{
return $this->__toArray(true);
}你可以这样使用它:
$matchUserArray = $matchUser->jsonSerialize();
// Output "hello"
echo $matchUserArray['data'][0]['metadata']['testitem']https://stackoverflow.com/questions/53943325
复制相似问题