【问题标题】:Force to add array into single element of JSON in PHP integrated to plugin强制在集成到插件的 PHP 中将数组添加到 JSON 的单个元素中
【发布时间】:2019-06-09 11:02:26
【问题描述】:

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

现在我需要在 3rd 方 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"
                  }
                ]
            }
        ]
    }
]

【问题讨论】:

    标签: php json wordpress


    【解决方案1】:

    您的代码每次通过时都会覆盖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++;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-12
      • 1970-01-01
      • 2012-06-18
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      • 2022-11-16
      相关资源
      最近更新 更多