Принудительно добавить массив в один элемент JSON в PHP, интегрированный в плагин - PullRequest
1 голос
/ 09 июня 2019

Этот вопрос основан на существующей записи в Принудительно добавить массив в один элемент JSON в PHP

Теперь мне нужно добавить пользовательскую функцию в сторонний плагин экспорта WordPress, чтобы получить желаемый вывод JSON. Так как у меня есть некоторые знания по PHP и массивам, я хотел бы получить помощь от ваших парней

Необходимо исправить еще две проблемы для этого кода:

1) order_item для postid = 32081 не может показать все элементы (всего 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"
                  }
                ]
            }
        ]
    }
]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...