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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?