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