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条回答 默认 最新

相关推荐 更多相似问题