简体   繁体   English

如何在php中创建对象数组?

[英]How to create object array in php?

I have following code: 我有以下代码:

PaypalRecurringPaymentProfile.php file PaypalRecurringPaymentProfile.php文件

public function createProfile(array $data)
{
if (array_key_exists('SUBSCRIBERNAME', $data))
  $nvp_params['SUBSCRIBERNAME'] = urlencode($data['SUBSCRIBERNAME']);
if (array_key_exists('PROFILESTARTDATE', $data)) {
  $nvp_params['PROFILESTARTDATE'] = urlencode($data['PROFILESTARTDATE']);
}
else {
  $_errors[] = 'PROFILESTARTDATE is required parameter';
}

}

Now my index.php from where i called above class 现在我从上面的类中调用的地方的index.php

$data = array();
$data['AMT']                = '9.99';
$data['ACCT']               = completed_number('4556', 16);
$data['CREDITCARDTYPE']     = PaypalRecurringPaymentProfile::cc_Visa;
$data['EXPDATE']            = '082014';
$data['CVV2']               = '086';
$data['FIRSTNAME']          = 'John';
$data['LASTNAME']           = 'Joe';
$data['STREET']             = '101 West 1th street apt 1';
$data['CITY']               = 'Brooklyn';
$data['STATE']              = 'NY';
$data['ZIP']                = '11201';
$data['PROFILESTARTDATE']   = '2012-10-01T00:00:00Z';
$data['DESC']               = 'Test of paypal recurring profile';
$data['BILLINGPERIOD']      = 'Month';
$data['BILLINGFREQUENCY']   = '1';
$data['TOTALBILLINGCYCLES'] = '12';
$data['TAXAMOUNT']          = '0.00';
$data['CURRENCYCODE']       = 'AUD';
$pp_profile                 = new PaypalRecurringPaymentProfile($api_username, $api_pasword, $api_signature, $api_version, $api_env);
$pp_create_profile          = $pp_profile->createProfile($data);
   print_r($pp_create_profile); exit;

I am creating object called $pp_profile and invoke method createProfile . 我正在创建名为$pp_profile对象,并调用方法createProfile

But when print $pp_create_profile it was blank 但是在打印$pp_create_profile时为空白

can anybody help me 有谁能够帮助我

You need to return array values. 您需要返回数组值。

    public function createProfile(array $data){

            $flag  =0;

            if (array_key_exists('SUBSCRIBERNAME', $data)){
              $nvp_params['SUBSCRIBERNAME'] = urlencode($data['SUBSCRIBERNAME']);
                $flag =1;
            }
            if (array_key_exists('PROFILESTARTDATE', $data)) {
              $nvp_params['PROFILESTARTDATE'] = urlencode($data['PROFILESTARTDATE']);
                 $flag =1;
            }
            else {
              $_errors[] = 'PROFILESTARTDATE is required parameter';

            }

            if($flag)
             return $nvp_params;
            else
             return $_errors;

  }

hi can you please check this, I hope this will helps you 您好,请检查一下,希望对您有所帮助

 $object = new stdClass();
 foreach ($array as $key => $value)
{
$object->$key = $value;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM