强制将数组添加到集成到插件的 PHP 中的单个 JSON 元素中 [英] Force to add array into single element of JSON in PHP integrated to plugin

查看:28
本文介绍了强制将数组添加到集成到插件的 PHP 中的单个 JSON 元素中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题基于 PHP 强制将数组添加到 JSON 的单个元素中

现在我需要在第 3 方 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) {全球$出口;$orders = woo_ce_get_orders('order', $export->args);if( !empty( $orders ) ) {if( !empty( $export->fields ) ) {$export->total_columns = $size = count( $export->columns );$输出= [交易日期"=>日期('Ymd'),新订单" =>[]];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;}}}}返回数组($输出);}

我从输出中得到什么

<预><代码>[{"transaction_date": "20190609",新订单":[{"postid": 32081,"标签": "22615","address": "19 RUE","邮政编码": "27450",订单项":{"ugs": "A0003",数量":6"}},{"postid": 32082,"标签": "22617","address": "2 impas","邮政编码": "12300",订单项":{"ugs": "B0002",数量":1"}}]}]

我需要什么

<预><代码>[{"transaction_date": "20190609",新订单":[{"postid": 32081,"标签": "22615","address": "19 RUE","邮政编码": "27450",订单项":[{"ugs": "A0001",数量":3"},{"ugs": "A0002",数量":1"},{"ugs": "A0003",数量":6"}]},{"postid": 32082,"标签": "22617","address": "2 impas","邮政编码": "12300",订单项":[{"ugs": "B0001",数量":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++;}

This question is based on the existing post at Force to add array into single element of JSON in PHP

Now i need to add some custom function into 3rd party WordPress export plugin to make my desired JSON output. Since i have some limit knowledge on the PHP and array, so would like some help from your guys

Two issue still need to fix for this code:

1) order_item for postid=32081 not able to show all items(total 3 order item), currently only last one is show e.g. ugs=A0003

2)Need to include array(or square bracket) to all the parameter under order_item

existing function to export 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);
} 

what i get from 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"
                }
            }
        ]
    }
]

what i need

[
    {
        "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"
                  }
                ]
            }
        ]
    }
]

解决方案

Your code overrides the order_item array everytime it passes. That's why only the last one shows up. Add a counter to add al the items as an array:

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++;
}

这篇关于强制将数组添加到集成到插件的 PHP 中的单个 JSON 元素中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆