dos8410 2018-06-26 01:45
浏览 36
已采纳

PHP在数组变量中添加条件(WooCommerce API)

Take for example the following piece of code from WooCommerce API Documentation. What I am trying to do is add some if conditions within the array. For example, I want the payment_details array to be a part of $data based on some if condition. Is this possible? How?

<?php
$data = [
    'order' => [
        'payment_details' => [
            'method_id' => 'bacs',
            'method_title' => 'Direct Bank Transfer',
            'paid' => true
        ],
        'billing_address' => [
            'first_name' => 'John',
            'last_name' => 'Doe',
            'address_1' => '969 Market',
            'address_2' => '',
            'city' => 'San Francisco',
            'state' => 'CA',
            'postcode' => '94103',
            'country' => 'US',
            'email' => 'john.doe@example.com',
            'phone' => '(555) 555-5555'
        ],
        'shipping_address' => [
            'first_name' => 'John',
            'last_name' => 'Doe',
            'address_1' => '969 Market',
            'address_2' => '',
            'city' => 'San Francisco',
            'state' => 'CA',
            'postcode' => '94103',
            'country' => 'US'
        ],
        'customer_id' => 2,
        'line_items' => [
            [
                'product_id' => 546,
                'quantity' => 2
            ],
            [
                'product_id' => 613,
                'quantity' => 1,
                'variations' => [
                    'pa_color' => 'Black'
                ]
            ]
        ],
        'shipping_lines' => [
            [
                'method_id' => 'flat_rate',
                'method_title' => 'Flat Rate',
                'total' => 10
            ]
        ]
    ]
];

print_r($woocommerce->post('orders', $data));
?>

The point is, instead of defining the entire array again, I want to put an if condition here:

'order' => [

if ($payment = 'xyz') {

'payment_details' => [
    'method_id' => 'bacs',
    'method_title' => 'Direct Bank Transfer',
    'paid' => true
],

} else {

'payment_details' => [
    'method_id' => 'monopoly',
    'method_title' => 'Monopoly',
    'paid' => true
],

}

Is it possible to concatenate the array using dot equals? .= Thanks.

  • 写回答

3条回答 默认 最新

  • doubi1797 2018-07-18 01:21
    关注

    Answering my own question. This is what I was looking for.

    $orderData = [];
                if ($payment == 'Monopoly') {
                    $orderData['order']['payment_details'] = [];
                    $orderData['order']['payment_details']['method_id'] = 'Monopoly';
                    $orderData['order']['payment_details']['method_title'] = 'Monopoly';
                    $orderData['order']['payment_details']['paid'] = true;
                    $orderData['order']['status'] = 'completed';
                }
                else {
                    $orderData['order']['status'] = 'pending';
                }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!