最好的方式转换PHP5.4数组解引用兼容PHP5.3

时间:2016-04-21 22:56:30

标签: php arrays drupal php-5.3 php-5.4

问题

需要将在PHP 5.4中运行的数组Deference转换为适用于PHP 5.3的版本。我无法更新我的实时网站PHP,所以我有点卡住了。我想要完成的是在提交表单时为Drupal 7创建优惠券代码。

我看过的地方:

PHP syntax for dereferencing function result很好的讨论。几乎看起来根本不可能。有几个很好的解决方案示例我无法转换为我的问题。

5.4 dereferencing to valid 5.3 array call与我的问题非常相似,但我无法找到使用它的方法。

原创适用于PHP 5.4:

$coupon->store_discount_reference = ['und'=>[['target_id'=>"57"]]];
$coupon->store_coupon_exclusive = ['und'=>[['value'=>"0"]]];
$coupon->store_coupon_conditions = ['und' => [
    [
        'condition_name'=>'store_coupon_usage_evaluate_usage',
        'condition_settings'=> ['max_usage'=>'1'],
        'conditions_negative'=>0,
        'remove_condition'=>'Remove'
    ],
]];

我尝试过什么

$coupon->store_discount_reference = array('und'=> array('target_id'=>"57"));
$coupon->store_coupon_exclusive = array('und'=> array('value'=>"0"));
$coupon->store_coupon_conditions = array('und' => array('condition_name'=>'store_coupon_usage_evaluate_usage','condition_settings'=> array('max_usage'=>'1'),'conditions_negative'=>0,'remove_condition'=>'Remove'));

Full Chunk

$coupon = new stdClass();
$coupon->type = "discount_coupon";
$coupon->coupon_id = '';
$coupon->code = $code;
$coupon->bulk = false;
$coupon->created = '';
$coupon->status = 1;
$coupon->uid = 0;
$coupon->is_new = true;
$coupon->commerce_discount_reference = ['und'=>[['target_id'=>"57"]]];
$coupon->commerce_coupon_exclusive = ['und'=>[['value'=>"0"]]];
$coupon->commerce_coupon_conditions = ['und' => [
    [
        'condition_name'=>'commerce_coupon_usage_evaluate_usage',
        'condition_settings'=> ['max_usage'=>'1'],
        'conditions_negative'=>0,
        'remove_condition'=>'Remove'
    ],
]];
commerce_coupon_save($coupon);

1 个答案:

答案 0 :(得分:2)

您似乎在任何地方都缺少数组级别

$coupon->store_discount_reference = array('und'=> array(array('target_id'=>"57")));

$coupon->store_coupon_exclusive = array('und'=> array( array('value'=>"0")));

$coupon->store_coupon_conditions = 
   array('und' => 
           array( 
                  array('condition_name'=>'store_coupon_usage_evaluate_usage',
                        'condition_settings'=> array('max_usage'=>'1'),
                        'conditions_negative'=>0,
                        'remove_condition'=>'Remove'
                   )
                )
  );