donglao4370 2016-01-26 12:17
浏览 123

为什么我的FOREACH循环在3次迭代后停止? (PHP和条纹)

My code is large as I am working on a subscription PHP plugin for WordPress. However, although most is done I now face a glitch that I can't fix. This is the flow:

  1. User fills in the signup form with shipping, billing and account details
  2. I create his account (WordPress)
  3. Once confirmed, I call \Stripe\Customer::create() with his email and card details
  4. Stripe creates the customer and sends it as JSON response
  5. I convert the Stripe Customer Object into an array called $object (->__toArray( true )) and then insert each field into my database

This is where my foreach comes into play. I made an error_log( $object ) before the foreach just to make sure there was everything I need and it's correct.

    [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
            )

    )

However, the foreach loop going through each of these $k/$v stops after two or three iterations and my actual page gives me a Chrome error message "err_empty_response.

[26-Jan-2016 11:58:02 UTC] id = cus_7n5diIO2wjeaYX
[26-Jan-2016 11:58:02 UTC] object = customer

My actual loop is like that:

    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;
    }

I am very confused as in why it stops at object (just a string) or account_balance (just an int, usually 0) and never goes to created and the rest.

EDIT: here is a screenshot of my entire error_log screenshot of error_log

EDIT2: to make sure it is not about these specific array keys or values, I changed the code and the same behaviour happens, it stops after a couple of iterations

[26-Jan-2016 13:02:13 UTC] id = (string) cus_7n6fFVw0w3awiy
[26-Jan-2016 13:02:13 UTC] created = (integer) 1453813333

Results from:

    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 );
    }

So it is not even about the keys or values....

  • 写回答

1条回答 默认 最新

  • dqunzip3183 2016-01-26 12:40
    关注

    I think the problem is the function formatvalue. This function doesn't accept object value.

    Try this:

    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;*/
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示