简体   繁体   中英

How to create object array in php?

I have following code:

PaypalRecurringPaymentProfile.php file

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

$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 .

But when print $pp_create_profile it was blank

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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