强制将数组添加到集成到插件的PHP中的JSON的单个元素中

时间:2019-06-09 11:02:26

标签: php json wordpress

此问题基于Force to add array into single element of JSON in PHP上的现有帖子

现在,我需要向第三方WordPress导出插件中添加一些自定义功能,以实现所需的JSON输出。由于我对PHP和数组有一定的了解,所以希望你们提供一些帮助

此代码仍需要修复两个问题:

1)postid = 32081的order_item无法显示所有商品(共3个订单商品),例如,当前仅显示最后一个商品。 ugs = A0003

2)需要在order_item下的所有参数中包含数组(或方括号)

导出json的现有功能

function woo_ce_export_dataset_override_order( $output = null, $export_type = null) {

    global $export;
    $orders = woo_ce_get_orders( 'order', $export->args );

    if( !empty( $orders ) ) {
        if( !empty( $export->fields ) ) {

            $export->total_columns = $size = count( $export->columns );

            $output = [
                "transaction_date" => date('Ymd'),
                "neworders" => []    
            ];

            if( !empty( $export->fields ) ) {
                foreach( $orders as $order ) {

                    $args = $export->args;

                    $order = woo_ce_get_order_data( $order, 'order', $args, array_keys( $export->fields ) );

                    foreach( array_keys( $export->fields ) as $key => $field ) {
                        if( isset( $order->$field ) && isset( $export->columns[$key] ) ) {

                            $post[ $export->columns[$key] ] = $order->$field;
                        }
                    }

                    if( !empty( $order->order_items ) ) {
                        foreach( $order->order_items as $order_item ) {
                            foreach( array_keys( $export->fields ) as $key => $field ) {
                                if( isset( $order_item->$field ) && isset( $export->columns[$key] ) ) {

                                    $post['order_item'][ $export->columns[$key] ] = $order_item->$field;
                                }
                            }
                        }
                    }
                    $output['neworders'][] = $post;
                }
            }
        }
    }
    return array($output);
} 

我从输出中得到什么

[
    {
        "transaction_date": "20190609",
        "neworders": [
            {
                "postid": 32081,
                "label": "22615",
                "address": "19 RUE",
                "postcode": "27450",
                "order_item": {
                    "ugs": "A0003",
                    "qty": "6"
                }
            },
            {
                "postid": 32082,
                "label": "22617",
                "address": "2 impas",
                "postcode": "12300",
                "order_item": {
                    "ugs": "B0002",
                    "qty": "1"
                }
            }
        ]
    }
]

我需要什么

[
    {
        "transaction_date": "20190609",
        "neworders": [
            {
                "postid": 32081,
                "label": "22615",
                "address": "19 RUE",
                "postcode": "27450",
                "order_item": [ 
                    {
                        "ugs": "A0001",
                        "qty": "3"
                    },
                    {
                        "ugs": "A0002",
                        "qty": "1"
                    },
                    {
                        "ugs": "A0003",
                        "qty": "6"
                    }
               ]
            },
            {
                "postid": 32082,
                "label": "22617",
                "address": "2 impas",
                "postcode": "12300",
                "order_item": [
                   {
                      "ugs": "B0001",
                      "qty": "1"
                  }
                ]
            }
        ]
    }
]

1 个答案:

答案 0 :(得分:0)

您的代码每次通过时都会覆盖 order_item 数组。这就是为什么只有最后一个出现。添加一个计数器以将所有项目添加为数组:

foreach( array_keys( $export->fields ) as $key => $field ) {
    if( isset( $order_item->$field ) && isset( $export->columns[$key] ) ) {
        $post['order_item'][$i][ $export->columns[$key] ] = $order_item->$field;
    }
    $i++;
}