我正在使用$input = json_decode($raw, true);
处理json字符串并将值输入到数组中,目的是将该数组作为采购订单收据输出。
除了" product_id"之外,它读取的大部分json都很好。在" line_items"的第一个(仅在此情况下)行项目数组中(以下样本)。 "订单项"本身就是" order"的一个子数组。
虽然它可以读取大部分数组$input["order"]["line_items"][0]
,但它会解释
json string sample:
"line_items": [{
"id": 124,
"subtotal": "332.50",
"subtotal_tax": "66.50",
"total": "0.00",
"total_tax": "0.00",
"price": "0.00",
"quantity": 1,
"tax_class": null,
"name": "Hydraflex Mattress",
"product_id": 1037,
"sku": "",
"meta": [{
"key": "pa_size",
"label": "Available Sizes",
"value": "UK Standard Single"
}]
var_dump($input["order"]["line_items"][0]);
的结果:
array(12) {
["id"]=> int(124)
["subtotal"]=> string(6) "332.50"
["subtotal_tax"]=> string(5) "66.50"
["total"]=> string(4) "0.00"
["total_tax"]=> string(4) "0.00"
["price"]=> string(4) "0.00"
["quantity"]=> int(1)
["tax_class"]=> NULL
["name"]=> string(18) "Hydraflex Mattress"
["product_id"]=> bool(true)
["sku"]=> string(0) ""
["meta"]=> array(1) {
[0]=> array(3) {
["key"]=> string(7) "pa_size"
["label"]=> string(15) "Available Sizes"
["value"]=> string(18) "UK Standard Single"
}
}
}
主要代码:
$prices = array(
5928 => 34.48, //Sleepsac;
5924 => 99.99,
5925 => 99.99,
5926 => 99.99,
5927 => 99.99,
1037 => 105.67
);
$raw = file_get_contents("webhook_sample.json");
$input = json_decode($raw, true);
// create an initial array with required fields set to null
$output = array(
"po_number" => NULL,
"item" => NULL,
"quantity" => NULL,
"invoice_price" => NULL,
"invoice_address" => NULL,
"delivery_address" => NULL,
"instructions" => NULL
);
function mailout($message) {
$to = 'example@example.com';
$subject = 'the subject';
$headers = 'From: example@example.com' . "\r\n" .
'Reply-To: example@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
// provides a check to see if the field is set and whether it's blank.
function conditional_return($input, $multi_input = false, $multi_last = false) {
if (isset($input)) {
if (!empty($input)) {
if ($multi_input && !$multi_last) {
// if it's not the last one
return $input.', ';
} else {
// if it is the last one or if it's a solo entry
return $input;
}
} else {
return "";
}
} else {
return "";
}
}
// create and populate array for address
$shipping_address = array();
$shipping_address["address_1"] = $input["order"]["shipping_address"]["address_1"];
$shipping_address["address_2"] = $input["order"]["shipping_address"]["address_2"];
$shipping_address["city"] = $input["order"]["shipping_address"]["city"];
$shipping_address["state"] = $input["order"]["shipping_address"]["state"];
$shipping_address["postcode"] = $input["order"]["shipping_address"]["postcode"];
// strip out any address elements that are not populated
foreach ($shipping_address as $key => $value) {
if ($value == "") {
unset($shipping_address[$key]);
}
}
// use remaining elements to create text address
$shipping_address_string = implode(", ", $shipping_address);
$number_of_items = count($input["order"]["line_items"]);
for ($i=0; $i < $number_of_items; $i++) {
if ($input["order"]["line_items"][$i]["product_id"] = 1037 ||
$input["order"]["line_items"][$i]["product_id"] = 0000 ||
$input["order"]["line_items"][$i]["product_id"] = 0000 ||) {
// Set the output parameters
echo "variable i is ".$i."<br>";
setlocale(LC_MONETARY,"en_EN");
$output["item"] = $input["order"]["line_items"][$i]["name"];
$output["quantity"] = $input["order"]["line_items"][$i]["quantity"];
$output["invoice_price"] = $prices[$input["order"]["line_items"][$i]["product_id"]];
$output["invoice_address"] = "Mammoth Technologies, Office 3, Yarm Road, Stockton-on-Tees, TS18 3NA, UK";
$output["delivery_address"] = $shipping_address_string;
$output["instructions"] = $input["order"]["note"];
// write output string
$output_string = "";
foreach ($output as $key => $value) {
$output_string .= $key.": ".$value."<br>";
}
echo $output_string;
var_dump($prices);
echo "<br><br>";
echo($input["order"]["line_items"][0]["product_id"]);
echo "<br><br>";
var_dump($input["order"]["line_items"][0]);
// mailout($output_string);
// file_put_contents("/outbox/dermatherapy_PO_".$input["order"]["id"]."_".$input["order"]["line_items"][$i]["product_id"].".txt", $output_string);
}
答案 0 :(得分:2)
我找到了问题所在。在您的情况下,您正在进行任务,而不是进行比较。你的代码是:
if ($input["order"]["line_items"][$i]["product_id"] = 1037 ||
$input["order"]["line_items"][$i]["product_id"] = 0000 ||
$input["order"]["line_items"][$i]["product_id"] = 0000 ||)
正确的代码应该是
if ($input["order"]["line_items"][$i]["product_id"] === 1037 ||
$input["order"]["line_items"][$i]["product_id"] === 0000 ||
$input["order"]["line_items"][$i]["product_id"] === 0000)
这是因为您将0000
值分配给产品ID,它是一个布尔值。