【发布时间】:2020-08-03 21:44:25
【问题描述】:
我收到关于循环 JSON 数组的循环阅读!
我已经解码了一个 JSON 字符串。它包含比萨饼的订单和客户信息。每个订单都在一个数组中,第一个称为 Array[0],第二个称为 Array[1],以此类推。 在每个数组中都有 [products],其中包含 Array[0] 和第一个披萨的详细信息,Array[1] 和第二个等的详细信息。
我终其一生都无法弄清楚如何访问 [products] 数组中的值。
在 [products] 中,其中一个值是 JSON 字符串本身,但我还没有担心这一点!
Print_R 给了我这个:
Array [0] => Array( [order_number] => 568
[products] => Array ( [0] => Array (
[item_no] => 1
[item_name] => Full Veggie Pizza
[qty] => 2
[woofood_meta] => {"original_price":"9.0"} )
[1] => Array (
[item_no] => 2
[item_name] => Full Veggie Pizza
[qty] => 1
[woofood_meta] => {"extra_options":{"Extras":
[{"id":61,"price":"\u00a30.50",
"price_float":0.5,"category":"Extras","name":"Jalape\u00f1os",
"hide_prices":false}]},"extra_options_price":0.5,"original_price":"9.0"} )
[2] => Array (
[item_no] => 3
[item_name] => Full Veggie Pizza
[qty] => 1
[woofood_meta] => {"extra_options":{"Extras":
[{"id":57,"price":"\u00a30.50",
"price_float":0.5,"category":"Extras","name":"Extra
veg 1)","hide_prices":false},
{"id":61,"price":"\u00a30.50","price_float":0.5,"category":"Extras","name":"Jalape\u00f1os","hide_prices":false }, {"id":62,"price":"\u00a30.00","price_float":0,"category":"Extras","name":"Chillies","hide_prices":false}]}, "additional_cmets":"Pineapple","extra_options_price":1,"original_price":"9.0"})
[3] => Array (
[item_no] => 4
[item_name] => Pepperoni Pizza
[qty] => 1
[woofood_meta] => {"original_price":"8.50"} )
[date_to_deliver] => 2020-07-25 00:00
[time_to_deliver] => 19:00
[full_name] => Freda People
[address] => 22 New St
[city] => Newtown
[postcode] => NT6 6NT
[phone] => 07123 456 789
[email] => freda@gmail.com
[customer_note] => Beware of the dog )
Array [1] => Array( [order_number] => 569 //这就是下一组数组的开始
我可以回显所有名称和地址详细信息,但 [products] 数组中没有任何内容。
我的代码是:
$url = 'orders(1).json';
$jsonobj = file_get_contents($url);
$arr = json_decode($jsonobj, true);
$element = $arr;
foreach($arr as $element) {
for($i=0;$i<count($element.products['$i']);$i++){
echo $element ['order_number']; "<br>";
echo $element ['products[$i].qty']; "<br>";
echo $element ['products[$i].item_name']; "<br>";
echo $element ['products[$i].woofood_meta.name']; "<br>";
echo $element ['products[$i].woofood_meta.additional_comments']; "<br>";
echo $element ['date_to_deliver']; "<br>";
echo $element ['time_to_deliver']; "<br>";
echo $element ['full_name']; "<br>";
}
}
请求的 json 数组:
[{"order_number":"573","products":[{"item_no":1,"item_name":"那不勒斯披萨","qty":"1","woofood_meta":"{"original_price ":"9.50"}"},{"item_no":2,"item_name":"Margherita Pizza","qty":"1","woofood_meta":"{"extra_options":{"Extras":[{ "id":59,"price":"\u00a31.00","price_float":1,"category":"Extras","name":"Extra cheese 1","hide_prices":false}]}, "extra_options_price":1,"original_price":"7.50"}"},{"item_no":3,"item_name":"奶酪大蒜面包 (v)","qty":"1","woofood_meta":" {"original_price":"4.5"}"}],"date_to_deliver":"2020-07-31 00:00","time_to_deliver":"19:00","full_name":"XXXX XXXX","address" :"ZZZZ ZZZZ","city":"VVVVVVVV","postcode":"123 456","phone":"1234567890","email":"xx@yy.com","customer_note":"我们是仍在屏蔽。感谢您的所有帮助"}
【问题讨论】:
-
忠告:忽略这是来自 JSON 的事实,并学习访问 PHP 数组的基础知识。
-
分享一个 JSON 示例,或许我们可以提供帮助
-
OP里面有print_r,想在这里发一行但是太长了。
-
@Anthony 请始终将您的数组/对象数据显示为
var_export()输出或 json 字符串——这样志愿者可以立即将您的样本数据复制粘贴到他们的测试环境中,并开始破解你的问题。当您使用print_r()或var_dump()时,志愿者必须浪费时间将数据准备成可以运行代码的东西。 (不知道这一点不用道歉,我只是在帮助您了解如何写出更清晰的问题并更快地获得更好的支持。)粘贴到问题中后,突出显示文本,然后按 Ctrl+K。 -
我在 OP 中添加了一行。感谢您的提醒!