编程语言
首页 > 编程语言> > php-如何清除此代码为关联数组?

php-如何清除此代码为关联数组?

作者:互联网

我一直在努力使以下代码可用,但我不知道它是什么类型的数据.

这部分是从ERPNext发布到我的Webhook的有效负载中提取的.我已经深入到数据的特定部分,但是似乎混合了不同的数据类型.

Array
(
    [{"stock_qty": 131.0, "base_price_list_rate": 4500.0, "image": "", "creation": "2019-04-02 22:33:56.356904", "base_amount": 589500.0, "qty": 131.0, "margin_rate_or_amount": 0.0, "rate": 4500.0, "total_weight": 0.0, "ensure_delivery_based_on_produced_serial_no": 0, "owner": "Administrator", "target_warehouse": null, "stock_uom": "Nos", "base_net_amount": 589500.0, "page_break": 0, "modified_by": "Administrator", "base_net_rate": 4500.0, "discount_percentage": 0.0, "item_name": "5277", "amount": 589500.0, "actual_qty": 0.0, "net_rate": 4500.0, "conversion_factor": 1.0, "base_rate_with_margin": 0.0, "supplier": null, "docstatus": 1, "prevdoc_docname": null, "uom": "Nos", "ordered_qty": 0.0, "doctype": "Sales Order Item", "description": "5277", "parent": "SAL-ORD-2019-00019", "gross_profit": 589500.0, "returned_qty": 0.0, "brand": null, "base_rate": 4500.0, "item_code": "5277", "produced_qty": 0.0, "projected_qty": -453.0, "warehouse": "Stores - AFSPK", "margin_type": "", "billed_amt": 0.0, "rate_with_margin": 0.0, "pricing_rule": null, "delivered_qty": 0.0, "delivered_by_supplier": 0, "discount_amount": 0.0, "price_list_rate": 4500.0, "weight_uom": null, "transaction_date": "2019-04-02", "name": "f94099637a", "idx": 1, "item_tax_rate": "{}", "item_group": "Products", "planned_qty": 0.0, "modified": "2019-04-02 22:34:00.282021", "weight_per_unit": 0.0, "work_order_qty": 0.0, "parenttype": "Sales Order", "customer_item_code": null, "blanket_order_rate": 0.0, "valuation_rate": 0.0, "net_amount": 589500.0, "blanket_order": null, "delivery_date": "2019-06-28", "parentfield": "items"}] => Array
        (
            [, "customer_address": null, "customer_name": "Muhammad", "name": "SAL-ORD-2019-00019", "title": "Muhammad"}] => 
        )

)

以下是来自有效负载的原始json解码数据

Array
(
    [data] => 
    [{"items": ] => Array
        (
            [{"stock_qty": 131.0, "base_price_list_rate": 4500.0, "image": "", "creation": "2019-04-02 22:33:56.356904", "base_amount": 589500.0, "qty": 131.0, "margin_rate_or_amount": 0.0, "rate": 4500.0, "total_weight": 0.0, "ensure_delivery_based_on_produced_serial_no": 0, "owner": "Administrator", "target_warehouse": null, "stock_uom": "Nos", "base_net_amount": 589500.0, "page_break": 0, "modified_by": "Administrator", "base_net_rate": 4500.0, "discount_percentage": 0.0, "item_name": "5277", "amount": 589500.0, "actual_qty": 0.0, "net_rate": 4500.0, "conversion_factor": 1.0, "base_rate_with_margin": 0.0, "supplier": null, "docstatus": 1, "prevdoc_docname": null, "uom": "Nos", "ordered_qty": 0.0, "doctype": "Sales Order Item", "description": "5277", "parent": "SAL-ORD-2019-00019", "gross_profit": 589500.0, "returned_qty": 0.0, "brand": null, "base_rate": 4500.0, "item_code": "5277", "produced_qty": 0.0, "projected_qty": -453.0, "warehouse": "Stores - AFSPK", "margin_type": "", "billed_amt": 0.0, "rate_with_margin": 0.0, "pricing_rule": null, "delivered_qty": 0.0, "delivered_by_supplier": 0, "discount_amount": 0.0, "price_list_rate": 4500.0, "weight_uom": null, "transaction_date": "2019-04-02", "name": "f94099637a", "idx": 1, "item_tax_rate": "{}", "item_group": "Products", "planned_qty": 0.0, "modified": "2019-04-02 22:34:00.282021", "weight_per_unit": 0.0, "work_order_qty": 0.0, "parenttype": "Sales Order", "customer_item_code": null, "blanket_order_rate": 0.0, "valuation_rate": 0.0, "net_amount": 589500.0, "blanket_order": null, "delivery_date": "2019-06-28", "parentfield": "items"}] => Array
                (
                    [, "customer_address": null, "customer_name": "Muhammad", "name": "SAL-ORD-2019-00019", "title": "Muhammad"}] => 
                )

        )

    [headers] => Array
        (
        )

)

我期待干净的东西

Array
(
    [stock_qty] => 131.0
    [base_price_list_rate] => 4500.0
    [creation] => 2019-04-02 22:33:56.356904
    [base_amount] => 589500.0
)

解决方法:

这有点奇怪,您必须将json(包括键和值)缝合在一起

尝试以下函数,它遍历已解码的数组,然后检测数据的开始{,将json缝合在一起,然后再次将其传递给json解码.

function decodeweird($data,$sub=false) {
    $return = '';
    foreach($data as $key => $value) {
        //detect the beginning of the data, or just append if we've already started
        if($sub || substr($key,0,1) == '{') {
            $return .= $key;
            if(!empty($value) && is_array($value)) $return .= decodeweird($value,true);
        }
    }
    return $sub ? $return:json_decode($return,true);
}

$received_data = <<<EOD
{
  "{\"items\": ": {
    "{\"stock_qty\": 131.0, \"base_price_list_rate\": 4500.0, \"image\": \"\", \"creation\": \"2019-04-02 22:33:56.356904\", \"base_amount\": 589500.0, \"qty\": 131.0, \"margin_rate_or_amount\": 0.0, \"rate\": 4500.0, \"total_weight\": 0.0, \"ensure_delivery_based_on_produced_serial_no\": 0, \"owner\": \"Administrator\", \"target_warehouse\": null, \"stock_uom\": \"Nos\", \"base_net_amount\": 589500.0, \"page_break\": 0, \"modified_by\": \"Administrator\", \"base_net_rate\": 4500.0, \"discount_percentage\": 0.0, \"item_name\": \"5277\", \"amount\": 589500.0, \"actual_qty\": 0.0, \"net_rate\": 4500.0, \"conversion_factor\": 1.0, \"base_rate_with_margin\": 0.0, \"supplier\": null, \"docstatus\": 1, \"prevdoc_docname\": null, \"uom\": \"Nos\", \"ordered_qty\": 0.0, \"doctype\": \"Sales Order Item\", \"description\": \"5277\", \"parent\": \"SAL-ORD-2019-00019\", \"gross_profit\": 589500.0, \"returned_qty\": 0.0, \"brand\": null, \"base_rate\": 4500.0, \"item_code\": \"5277\", \"produced_qty\": 0.0, \"projected_qty\": -453.0, \"warehouse\": \"Stores - AFSPK\", \"margin_type\": \"\", \"billed_amt\": 0.0, \"rate_with_margin\": 0.0, \"pricing_rule\": null, \"delivered_qty\": 0.0, \"delivered_by_supplier\": 0, \"discount_amount\": 0.0, \"price_list_rate\": 4500.0, \"weight_uom\": null, \"transaction_date\": \"2019-04-02\", \"name\": \"f94099637a\", \"idx\": 1, \"item_tax_rate\": \"{}\", \"item_group\": \"Products\", \"planned_qty\": 0.0, \"modified\": \"2019-04-02 22:34:00.282021\", \"weight_per_unit\": 0.0, \"work_order_qty\": 0.0, \"parenttype\": \"Sales Order\", \"customer_item_code\": null, \"blanket_order_rate\": 0.0, \"valuation_rate\": 0.0, \"net_amount\": 589500.0, \"blanket_order\": null, \"delivery_date\": \"2019-06-28\", \"parentfield\": \"items\"}": {
      ", \"customer_address\": null, \"customer_name\": \"Muhammad\", \"name\": \"SAL-ORD-2019-00019\", \"title\": \"Muhammad\"}": null
    }
  },
  "headers": {
  }
}
EOD;
$received = json_decode($received_data,true);

var_dump(decodeweird($received));

标签:erpnext,json,php,frappe
来源: https://codeday.me/bug/20191211/2105344.html