将Array php转换为json

时间:2016-10-31 00:26:14

标签: php json

我想使用curl将此数组发布到网站,所以我必须先将它转换为json。我使用了json_encode,但格式错误。

   $inv=Array
    ("sales_invoice" => Array
            (
                "transaction_date" => $date,
                "transaction_lines_attributes" => [ Array
                    (
                                "quantity" => 1,
                                "rate" => $price,
                                "discount" => 0,
                                "product_name" => "$ordersku",
                                "tax" => false
                    )],
                "address" => "JL. Gatot Subroto 55, Jakarta, Jawa Barat, 11739",
                "term_name" => "Net 30",
                "deposit_to_name" => "Cash",
                "warehouse_name" => "",
                "due_date" => "$due2",
                "shipping_date" => "$date",
                "shipping_price" => "0.0",
                "shipping_address" => "ship_address",
                "is_shipped" => true,
                "ship_via" => "jne",
                "reference_no" => "$ordernumber",
                "tracking_no" => "",
                "deposit" => "0",
                "person_name" => "Lazada",
                "discount_type_name" => "Value",
                "discount_unit" => 0,
                "custom_id" => "",
                "tax_name" => "Tax 0%",
                "email" => "milleniabedding@gmail.com",
                "transaction_no" => "$orderitemid",
            ));

json格式必须像这样

{\n  \"sales_invoice\": {\n    \"transaction_date\": \"2015-10-23\",\n    \"transaction_lines_attributes\": [\n      {\n        \"quantity\": 1,\n        \"rate\": 25000000,\n        \"discount\": 0,\n        \"product_name\": \"Sales\",\n        \"tax\": true\n      }\n    ],\n    \"address\": \"JL. Gatot Subroto 55, Jakarta, Jawa Barat, 11739\",\n    \"term_name\": \"Cash_on_Delivery\",\n    \"deposit_to_name\": \"Cash\",\n    \"warehouse_name\": \"New Warehouse\",\n    \"due_date\": \"2015-10-23\",\n    \"shipping_date\": \"2016-09-15\",\n    \"shipping_price\": \"0.0\",\n    \"shipping_address\": \"ship_address\",\n    \"is_shipped\": true,\n    \"ship_via\": \"jne\",\n    \"reference_no\": \"REF1\",\n    \"tracking_no\": \"TR01\",\n    \"deposit\": \"25000000\",\n    \"person_name\": \"Per\",\n    \"discount_type_name\": \"Value\",\n    \"discount_unit\": 30000,\n    \"custom_id\": \"SalesInvoice1\",\n    \"tax_name\": \"PPN Tes\",\n    \"email\": \"sale@example.com\",\n    \"transaction_no\": \"4SH4LFS\"\n  }\n}";

干杯

1 个答案:

答案 0 :(得分:1)

我可以使用json_encodestr_replace完成90%,因为我已展示here

$price  = 15.50;
$ordersku   = "TEST_SKU";
$due2   = "due?";
$date   = "2015-10-23";
$ordernumber    = "1900000001";
$orderitemid    = "1500";

$inv=array("sales_invoice" => array
        (
            "transaction_date" => $date,
            "transaction_lines_attributes" => [ array
                (
                            "quantity" => 1,
                            "rate" => $price,
                            "discount" => 0,
                            "product_name" => "$ordersku",
                            "tax" => false
                )],
            "address" => "JL. Gatot Subroto 55, Jakarta, Jawa Barat, 11739",
            "term_name" => "Net 30",
            "deposit_to_name" => "Cash",
            "warehouse_name" => "",
            "due_date" => "$due2",
            "shipping_date" => "$date",
            "shipping_price" => "0.0",
            "shipping_address" => "ship_address",
            "is_shipped" => true,
            "ship_via" => "jne",
            "reference_no" => "$ordernumber",
            "tracking_no" => "",
            "deposit" => "0",
            "person_name" => "Lazada",
            "discount_type_name" => "Value",
            "discount_unit" => 0,
            "custom_id" => "",
            "tax_name" => "Tax 0%",
            "email" => "milleniabedding@gmail.com",
            "transaction_no" => "$orderitemid",
        ));

$json = json_encode($inv);

$replacement = array(
    "search" =>  array("{",  "}",  '"', ","),
    "replace" => array('{\n','}\n','\"',',\n')
    );

var_dump(str_replace($replacement["search"],$replacement["replace"],$json));

str_replace的结果是

{\n\"sales_invoice\":{\n\"transaction_date\":\"2015-10-23\",\n\"transaction_lines_attributes\":[{\n\"quantity\":1,\n\"rate\":15.5,\n\"discount\":0,\n\"product_name\":\"TEST_SKU\",\n\"tax\":false}\n],\n\"address\":\"JL. Gatot Subroto 55,\n Jakarta,\n Jawa Barat,\n 11739\",\n\"term_name\":\"Net 30\",\n\"deposit_to_name\":\"Cash\",\n\"warehouse_name\":\"\",\n\"due_date\":\"due?\",\n\"shipping_date\":\"2015-10-23\",\n\"shipping_price\":\"0.0\",\n\"shipping_address\":\"ship_address\",\n\"is_shipped\":true,\n\"ship_via\":\"jne\",\n\"reference_no\":\"1900000001\",\n\"tracking_no\":\"\",\n\"deposit\":\"0\",\n\"person_name\":\"Lazada\",\n\"discount_type_name\":\"Value\",\n\"discount_unit\":0,\n\"custom_id\":\"\",\n\"tax_name\":\"Tax 0%\",\n\"email\":\"milleniabedding@gmail.com\",\n\"transaction_no\":\"1500\"}\n}\n

您只需创建普通的json字符串,然后使用str_replace添加\您需要的地方。问题是这个方法不会为你插入那些空格/标签。

如果你确实需要那些标签,那么最好的方法是使用递归

function altJSonEncode($arr,$tabNo=1)
{
    $returnStr = "";
    $genTabs = "";
    for($i = 0; $i < $tabNo; $i++)
    {
        $genTabs .= "  ";   
    }
    foreach($arr as $key => $val)
    {
        $returnStr .= $genTabs.'\"'.$key.'\": ';
        if(is_array($val))
        {
            $tabNo += 1;
            $returnStr .= altJSonEncode($val,$tabNo);
        }
        else
        {
            $returnStr .= '\"'.$val.'\",\n';
        }
    }
    return '{\n'.$genTabs.$returnStr.$genTabs.'}\n';
}

$price  = 25000000;
$ordersku   = "Sales";
$due2   = "2015-10-23";
$date   = "2015-10-23";
$ordernumber    = "1900000001";
$orderitemid    = "1500";

$inv=array("sales_invoice" => array
        (
            "transaction_date" => $date,
            "transaction_lines_attributes" => [ array
                (
                            "quantity" => 1,
                            "rate" => $price,
                            "discount" => 0,
                            "product_name" => "$ordersku",
                            "tax" => false
                )],
            "address" => "JL. Gatot Subroto 55, Jakarta, Jawa Barat, 11739",
            "term_name" => "Net 30",
            "deposit_to_name" => "Cash",
            "warehouse_name" => "",
            "due_date" => "$due2",
            "shipping_date" => "$date",
            "shipping_price" => "0.0",
            "shipping_address" => "ship_address",
            "is_shipped" => true,
            "ship_via" => "jne",
            "reference_no" => "$ordernumber",
            "tracking_no" => "",
            "deposit" => "0",
            "person_name" => "Lazada",
            "discount_type_name" => "Value",
            "discount_unit" => 0,
            "custom_id" => "",
            "tax_name" => "Tax 0%",
            "email" => "milleniabedding@gmail.com",
            "transaction_no" => "$orderitemid",
        ));

var_dump(altJSonEncode($inv));

example

我们在这里做的是在第一次调用它时将数组传递给函数但不是第二个参数(因此它具有默认值)。在我们的函数中,如果我们检测每个循环中当前索引的值是一个数组,如果是这样我们再次调用函数发送我们最多的数组并传递$tabNo的更新值

然而,这仍然不完美,因为

  • 输出所有$keys,因为transaction_lines_attributes是一个数组数组,为第一个且唯一的项输出键0
  • 在示例链接中输出了4个空格(不确定是否该网站正在进行,但即使我进行匹配,与示例输出相比,空格数也不匹配。
  • 所有内容都被视为字符串,因此像quantity这样的整数将围绕值"。同样,如果tax为false,则返回一个空字符串(不确定为什么它不是0,因为false = 0且true = 1且true仍然输出1,也许它只是boolean =&gt;字符串转换是如何工作的,因为空PHP中的字符串""也是假的)

但这些小问题可以通过更多的工作来解决。但是这两种方法都需要自己的解码功能,而不是使用json_decode对我来说,如果你想要json,那么对我来说似乎比它的价值更麻烦。 json_decode可以正常使用json_encode的输出,反之亦然