我的代码很大,因为我正在为WordPress订阅PHP插件。然而,虽然大多数已经完成,但我现在面临一个我无法修复的故障。这是流程:
这是我的foreach发挥作用的地方。我在foreach之前做了一个error_log($ object),以确保我需要的一切都是正确的。
[26-Jan-2016 12:01:58 UTC] Array
(
[id] => cus_7n5hEVJs2btpg8
[object] => customer
[account_balance] => 0
[created] => 1453809716
[currency] =>
[default_source] => card_17XVWCIh4e6dX4rQYJ8JzE7e
[delinquent] =>
[description] =>
[discount] =>
[email] => hello@xfsgfs.com
[livemode] =>
[metadata] => Array
(
)
[shipping] =>
[sources] => Array
(
[object] => list
[data] => Array
(
[0] => Array
(
[id] => card_17XVWCIh4e6dX4rQYJ8JzE7e
[object] => card
[address_city] =>
[address_country] =>
[address_line1] =>
[address_line1_check] =>
[address_line2] =>
[address_state] =>
[address_zip] => W12 8QQ
[address_zip_check] => pass
[brand] => Visa
[country] => US
[customer] => cus_7n5hEVJs2btpg8
[cvc_check] => pass
[dynamic_last4] =>
[exp_month] => 1
[exp_year] => 2019
[fingerprint] => FIYdIZ89jqCdZ9G4
[funding] => credit
[last4] => 4242
[metadata] => Array
(
)
[name] => Lazhar Ich
[tokenization_method] =>
)
)
[has_more] =>
[total_count] => 1
[url] => /v1/customers/cus_7n5hEVJs2btpg8/sources
)
[subscriptions] => Array
(
[object] => list
[data] => Array
(
)
[has_more] =>
[total_count] => 0
[url] => /v1/customers/cus_7n5hEVJs2btpg8/subscriptions
)
)
然而,经过两次或三次迭代后,经过每个$ k / $ v的foreach循环停止,而我的实际页面给出了Chrome错误消息“err_empty_response。
[26-Jan-2016 11:58:02 UTC] id = cus_7n5diIO2wjeaYX
[26-Jan-2016 11:58:02 UTC] object = customer
我的实际循环是这样的:
public function reconcile( $object = null, $action = null ) {
$exists = $this->exists();
if( $exists ) {
if( !$object )
$object = $this->retrieve();
if( !is_array( $object ) && !($object instanceof stdClass) )
$object = $object->__toArray( true );
error_log( print_r( $object, true ) );
foreach ( $object as $key => $value ) {
error_log( $key . ' = ' . $this->formatvalue( $value, false, true ) );
$this->$key = $this->formatvalue( $value, false, true );
}
error_log( 'update to be called next.' );
$this->update();
}
}
public function formatvalue( $value, $serialize = true, $nulltoemptystring = false ) {
if( !isset( $value ) )
return $nulltoemptystring ? '' : null;
elseif( is_int( $value ) && $value == '0' )
return 0;
elseif( is_array( $value ) )
return $serialize ? serialize( $value ) : $value;
elseif( is_bool( $value ) )
return $value ? 1 : 0;
elseif( trim( $value ) === '' )
return '';
else
return $value;
}
我很困惑,因为它停在对象(只是一个字符串)或account_balance(只是一个int,通常为0),永远不会创建和其余的。
EDIT2:为了确保它不是关于这些特定的数组键或值,我更改了代码并发生相同的行为,它在几次迭代后停止
[26-Jan-2016 13:02:13 UTC] id = (string) cus_7n6fFVw0w3awiy
[26-Jan-2016 13:02:13 UTC] created = (integer) 1453813333
结果来自:
if( $key != 'object' && $key != 'account_balance' ) {
$v = $this->formatvalue( $value, false, true );
error_log( $key . ' = (' . gettype( $v ) . ') ' . $v );
$this->$key = $this->formatvalue( $value, false, true );
}
所以它甚至不是键或值....
答案 0 :(得分:0)
我认为问题是函数formatvalue。 此功能不接受对象值。
试试这个:
public function formatvalue( $value, $serialize = true, $nulltoemptystring = false ) {
if( !isset( $value ) )
return $nulltoemptystring ? '' : null;
elseif( is_int( $value ) && $value == '0' )
return 0;
elseif( is_array( $value ) || is_object( $value ) )
return $serialize ? serialize( $value ) : $value;
elseif( is_bool( $value ) )
return $value ? 1 : 0;
// you can replace by a simple trim
return trim( $value );
/*elseif( trim( $value ) === '' )
return '';
else
return $value;*/
}