如何将此代码清理为关联数组?

How can I clean this code to an associative array?

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

这部分是从 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_decode。

例如:

$jsonData = "{"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"}"

$assosArray = json_decode($jsonData, true);

注意true,关联数组必须设置为true!

这有点奇怪,您必须将 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));