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



我正在定制一个WordPress插件来导出JSON文件供第三方使用。

我需要在"order_item"中添加一个数组,其中它只包含一个项目。对于多个订单项,将自动添加数组(方括号(。

我尝试不同的方法来包含数组到 $child->addChild( 'order_item'

(,例如 (array($child->addChild( 'order_item' (,但它应该是错误的方法。

生成 JSON 输出的函数如下所示:

function woo_ce_export_dataset_override_order( $output = null ) {
    global $export;
    if( !empty( $export->fields ) ) {
        $child = $output->addChild( 'transaction_date', date("Ymd", time()) );
        foreach( $orders as $order ) {
            $child = $output->addChild( apply_filters( 'woo_ce_export_xml_order_node', 'neworders' ) );
            $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] ) ) {
                    if( !is_array( $field ) ) {
                        $child->addChild( apply_filters( 'woo_ce_export_xml_order_label', sanitize_key( $export->columns[$key] ), $export->columns[$key] ), esc_html( woo_ce_sanitize_xml_string( $order->$field ) ) );
                    }
                }
            }
            if( !empty( $order->order_items ) ) {
                foreach( $order->order_items as $order_item ) {
                    $order_item_child = $child->addChild( 'order_item' );
                    foreach( array_keys( $export->fields ) as $key => $field ) {
                        if( isset( $order_item->$field ) && isset( $export->columns[$key] ) ) {
                            if( !is_array( $field ) ) {
                                $order_item_child->addChild( apply_filters( 'woo_ce_export_xml_order_label', sanitize_key( $export->columns[$key] ), $export->columns[$key] ), esc_html( woo_ce_sanitize_xml_string( $order_item->$field ) ) );
                            }
                        }
                    }
                }
            }
        }
        // Allow Plugin/Theme authors to add support for sorting Orders
        $output = apply_filters( 'woo_ce_orders_output', $output, $orders );
    }
    return $output;
}   

这是我从输出中得到的:

{
    "transaction_date": "20190607",
    "neworders": [
        {
            "postid": "12081",
            "order_item": [
                {
                    "ugs": "SAM1222",
                    "qty": "3"
                },
                {
                    "ugs": "NOK8777",
                    "qty": "3"
                }
            ]
        },
        {
            "postid": "12082",
            "order_item": {
                "ugs": "SON7411",
                "qty": "1"
            }
        }
    ]
}

我希望在 postid 的order_item中包含数组的内容:12082

{
    "transaction_date": "20190607",
    "neworders": [
        {
            "postid": "12081",
            "order_item": [
                {
                    "ugs": "SAM1222",
                    "qty": "3"
                },
                {
                    "ugs": "NOK8777",
                    "qty": "3"
                }
            ]
        },
        {
            "postid": "12082",
            "order_item": [
                {
                    "ugs": "SON7411",
                    "qty": "1"
                }
            ]
        }
    ]
}

看起来您正在生成XML(使用SimpleXML(而不是JSON。我想你使用 json_encode(( 将 SimpleXML 对象的调试/vardump 输出编译为 JSON。

此时,SimpleXML 无法知道单个元素可能具有同级元素。

我建议你直接生成 JSON 的结构。使用PHP很简单。下面是一个简化的示例(没有数据获取和过滤器(:

// simplified demo data
$orders = [
  '12081' => [
      'SAM1222' => 3,
      'NOK8777' => 3
  ], 
  '12082' => [
      'SON7411' => 1
  ]   
];
// associative arrays get encoded as objects in JSON
$result = [
  "transaction_date" => date('Ymd'),
  "neworders" => []    
];
foreach($orders as $orderId => $order) {
   $post = [
      'postid' => $orderId,
      'order_item' => []
   ];
   foreach ($order as $itemId => $quantity) {
       // numerical arrays will be encoded as arrays in JSON
       $post['order_item'][] = [
           "ugs" => $itemId,
           "qty" => $quantity
       ];
   }
   $result['neworders'][] = $post;
}
echo json_encode($result, JSON_PRETTY_PRINT);

输出:

{ 
  "transaction_date": "20190608", 
  "neworders": [ 
    { 
      "postid": 12081, 
      "order_item": [ 
        { 
          "ugs": "SAM1222", 
          "qty": 3 
        }, 
        {
          "ugs": "NOK8777",
          "qty": 3 
        } 
      ] 
    }, 
    { 
      "postid": 12082, 
      "order_item": [ 
        { 
          "ugs": "SON7411",
          "qty": 1 
        } 
      ] 
    } 
  ] 
}

最新更新