无法弄清楚如何使用PHP正确解析JSON数组。
在我的脚本中,我有一个foreach语句,用于解析本地JSON文件,提取数据并将其插入数据库中。
除了星期和营业时间的解析之外,其他一切都很好。
这就是我所做的:
$openingHours = $json_data['openingHours'];
每个json文件都可能返回一个包含不同行的数组,如下所示:
example 1:
"openingHours" : [
"Su 09:00 - 23:59",
"Mo 00:00 - 23:59",
"Tu 00:00 - 13:00, 15:30 - 19:30",
"We 08:30 - 13:00, 15:30 - 19:30",
"Th 08:30 - 13:00, 15:30 - 19:30",
"Fr 08:30 - 13:00, 15:30 - 19:30",
"Sa 08:30 - 13:00, 15:30 - 19:30" ]
exampe 2:
"openingHours" : [
"Mo 08:00 - 13:00, 16:00 - 20:00",
"Tu 08:00 - 13:00, 16:00 - 20:00",
"We 08:00 - 13:00, 16:00 - 20:00",
"Th 08:00 - 13:00, 16:00 - 20:00",
"Fr 08:00 - 13:00, 16:00 - 20:00",
"Sa 08:00 - 13:00, 16:00 - 20:00" ]
example 3:
"openingHours" : [
"Mo 08:00 - 13:00, 16:00 - 20:00",
"We 08:00 - 13:00, 16:00 - 20:00",
"Th 08:00 - 13:00, 16:00 - 20:00",
"Sa 08:00 - 13:00, 16:00 - 20:00" ]
您可以看到,在第一个json中,我有7天,在第二个示例中,星期一至星期六有6天,在第三个示例中,Mo-We-Th-Sa
有4天>所以我将创建一个foreach语句来输出数据;
foreach ($openingHours as $k_day=>$v_day) {
$openHrs = $openingHours[$k_day];
echo print_r($openHrs)."<br>";
}
它产生这样的输出:
FILE 1:
Mo 08:30 - 12:30, 16:00 - 20:00
Tu 08:30 - 12:30, 16:00 - 20:00
We 08:30 - 12:30, 16:00 - 20:00
Th 16:00 - 20:00
Fr 08:30 - 12:30, 16:00 - 20:00
Sa 08:30 - 12:30, 16:00 - 20:00
FILE 2:
Su 09:00 - 23:59
Mo 00:00 - 23:59
Tu 00:00 - 13:00, 15:30 - 19:30
We 08:30 - 13:00, 15:30 - 19:30
Th 08:30 - 13:00, 15:30 - 19:30
Sa 08:30 - 13:00, 15:30 - 19:30
FILE N:
.....
这时,我需要创建一些规则以正确格式化并从数组中提取日期和营业时间。
首先,我必须声明从星期日开始的一周中的几天
0 - Su
1 - Mo
2 - Tu
3 - We
4 - Th
5 - Fr
6 - Sa
然后,检查数组中是否存在所有星期字符串(Su-Mo -Tu ...),如果缺少某天,则将它们以正确的位置添加到列表中。
如本例所示
"openingHours" : [
"Mo 08:00 - 13:00, 16:00 - 20:00",
"We 08:00 - 13:00",
"Th 08:00 - 13:00, 16:00 - 20:00",
"Sa 08:00 - 13:00, 16:00 - 20:00" ]
日子:Su-Tu-Fr,不存在(这表示这几天商店关门了)。
在“星期三”,它仅在08:00至13:00之间开放,并且第3和第4个时段不存在。这意味着我需要插入2个时隙。 显然,对于休假日,我也需要插入时隙(“ 00:00-00:00、00:00-00:00”)。
0 - <-- insert
1 - Mo
2 - <-- insert
3 - We --> add "00:00 - 00:00"
4 - Th
5 - <-- insert
6 - Sa
总结 全天行及其广告位必须填充正确的数据。
最后,在将数据传递到数据库之前,我需要正确地参数化变量。
这是我要在其中放置数据的表的CREATE TABLE语句:
CREATE TABLE `business_hours_temp` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`place_id` INT(11) NOT NULL,
`day` INT(1) NOT NULL,
`open_1` TIME NOT NULL,
`close_1` TIME NOT NULL,
`open_2` TIME NOT NULL,
`close_2` TIME NOT NULL,
PRIMARY KEY (`id`),
INDEX `place_id` (`place_id`)
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB
AUTO_INCREMENT=12;
这是INSERT INTO语句
$stmt_hours = $conn->prepare(
'INSERT INTO business_hours_temp( place_id, day, open_1, close_1, open_2, close_2 )
VALUES(:place_id, :field_id, :field_value)');
$stmt_hours->bindValue(':place_id', $place_id);
$stmt_hours->bindValue(':day', $day);
$stmt_hours->bindValue(':open_1', $open_1);
$stmt_hours->bindValue(':close_1', $close_1);
$stmt_hours->bindValue(':open_2', $open_2);
$stmt_hours->bindValue(':close_2', $close_2);
$stmt_hours->execute();
我需要获取$day - $open_1 - $close_1 - $open_2 - $close_2
的值
谢谢大家的帮助。
答案 0 :(得分:2)
<?php
$openingHours1 = [
"Su 09:00 - 23:59",
"Mo 00:00 - 23:59",
"Tu 00:00 - 13:00, 15:30 - 19:30",
"We 08:30 - 13:00, 15:30 - 19:30",
"Th 08:30 - 13:00, 15:30 - 19:30",
"Fr 08:30 - 13:00, 15:30 - 19:30",
"Sa 08:30 - 13:00, 15:30 - 19:30" ];
$openingHours3 = [
"Mo 08:00 - 13:00, 16:00 - 20:00",
"We 08:00 - 13:00, 16:00 - 20:00",
"Th 08:00 - 13:00, 16:00 - 20:00",
"Sa 08:00 - 13:00, 16:00 - 20:00" ];
$openingHours2 = [
"Mo 08:00 - 13:00, 16:00 - 20:00",
"Tu 08:00 - 13:00, 16:00 - 20:00",
"We 08:00 - 13:00, 16:00 - 20:00",
"Th 08:00 - 13:00, 16:00 - 20:00",
"Fr 08:00 - 13:00, 16:00 - 20:00",
"Sa 08:00 - 13:00, 16:00 - 20:00" ];
function parse($openingHours)
{
$resultList = [];
$daysLut = [
'Su' => 0,
'Mo' => 1,
'Tu' => 2,
'We' => 3,
'Th' => 4,
'Fr' => 5,
'Sa' => 6,
];
$counter = 0;
$dayNoLut = [];
foreach ($openingHours as $oppening) {
$open1 = null;
$open2 = null;
$dayLetter = substr($oppening, 0, 2);
$dayNo = $daysLut[$dayLetter];
$open1 = substr($oppening, 3, 13);
if(strlen($oppening) > 18) {
$open2 = substr($oppening, 18);
}
if(empty($open2)) {
$open2 = "00:00 - 00:00";
}
$result['dayNo'] = $dayNo;
$result['open1'] = $open1;
$result['open2'] = $open2;
$dayNoLut[$dayNo] = $counter;
$resultList[] = $result;
$counter++;
}
$resultFilledList = [];
$resultListCount = count($resultList);
if($resultListCount < 7) {
$result = [];
$result['open1'] = "00:00 - 00:00";
//remove this line if for a cloed day you want only one 00:00 - 00:00
$result['open2'] = "00:00 - 00:00";
for($i=0; $i<7; $i++) {
$result['dayNo'] = $i;
if(key_exists($i, $dayNoLut)) {
$resultFilledList[$i] = $resultList[$dayNoLut[$i]];
} else {
$resultFilledList[$i] = $result;
}
}
} else {
$resultFilledList = $resultList;
}
return $resultFilledList;
}
$resultFilledList = parse($openingHours3);
print_r($resultFilledList);
$daysLut2 = [
0 => 'Su',
1 => 'Mo',
2 => 'Tu',
3 => 'We',
4 => 'Th',
5 => 'Fr',
6 => 'Sa',
];
$lineList = [];
foreach($resultFilledList as $result) {
$line = '';
$line .= $daysLut2[$result['dayNo']];
$line .= ' ' . $result['open1'];
$line .= ', ' . $result['open2'];
$lineList[] = $line;
}
print_r($lineList);
$lineList = [];
foreach($resultFilledList as $result) {
$line = '';
$line = $result['dayNo'];
$line .= ' - ';
$line .= $daysLut2[$result['dayNo']];
$line .= ' ' . $result['open1'];
$line .= ', ' . $result['open2'];
$lineList[] = $line;
}
print_r($lineList);
给出输出:
Array
(
[0] => Array
(
[open1] => 00:00 - 00:00
[open2] => 00:00 - 00:00
[dayNo] => 0
)
[1] => Array
(
[dayNo] => 1
[open1] => 08:00 - 13:00
[open2] => 16:00 - 20:00
)
[2] => Array
(
[open1] => 00:00 - 00:00
[open2] => 00:00 - 00:00
[dayNo] => 2
)
[3] => Array
(
[dayNo] => 3
[open1] => 08:00 - 13:00
[open2] => 16:00 - 20:00
)
[4] => Array
(
[dayNo] => 4
[open1] => 08:00 - 13:00
[open2] => 16:00 - 20:00
)
[5] => Array
(
[open1] => 00:00 - 00:00
[open2] => 00:00 - 00:00
[dayNo] => 5
)
[6] => Array
(
[dayNo] => 6
[open1] => 08:00 - 13:00
[open2] => 16:00 - 20:00
)
)
Array
(
[0] => Su 00:00 - 00:00, 00:00 - 00:00
[1] => Mo 08:00 - 13:00, 16:00 - 20:00
[2] => Tu 00:00 - 00:00, 00:00 - 00:00
[3] => We 08:00 - 13:00, 16:00 - 20:00
[4] => Th 08:00 - 13:00, 16:00 - 20:00
[5] => Fr 00:00 - 00:00, 00:00 - 00:00
[6] => Sa 08:00 - 13:00, 16:00 - 20:00
)
Array
(
[0] => 0 - Su 00:00 - 00:00, 00:00 - 00:00
[1] => 1 - Mo 08:00 - 13:00, 16:00 - 20:00
[2] => 2 - Tu 00:00 - 00:00, 00:00 - 00:00
[3] => 3 - We 08:00 - 13:00, 16:00 - 20:00
[4] => 4 - Th 08:00 - 13:00, 16:00 - 20:00
[5] => 5 - Fr 00:00 - 00:00, 00:00 - 00:00
[6] => 6 - Sa 08:00 - 13:00, 16:00 - 20:00
)
只需使用
$resultFilledList = parse($openingHours3);
其中oppeningHours3
是一个数组。因此,如果您将其包含在json中,请使用$openingHours3 = json_decode($data, true);
,
然后
像我在代码中所做的那样,使用foreach循环打印或遍历它。
答案 1 :(得分:0)
感谢Jimmix! 可行!
我只是做了一些小的更改以捕获所需变量的值。 该代码具有注释。再次向你致敬。
foreach($resultFilledList as $result) {
$line = '';
$line .= $daysLut2[$result['dayNo']];
$line .= ' ' . $result['open1'];
$line .= ', ' . $result['open2'];
$lineList[] = $line;
//FOR STORING DATA IN DB
//get DAY - INT
$day = $daysLut2[$result['dayNo']];
// SPLIT/EXPLODE Main slot 1 and 2
// and get time_slot 1,2, 3 and 4
// trim the EXPLODE and add ":00" for proper MySQL TIME type
$main_slot_1 = $result['open1'];
$main_slot_1_expl = explode('-',$main_slot_1,2);
$main_slot_2 = $result['open2'];
$main_slot_2_expl = explode('-',$main_slot_2,2);
$time_slot_1 = trim($main_slot_1_expl[0], ' ').":00";
$time_slot_2 = trim($main_slot_1_expl[1], ' ').":00";
$time_slot_3 = trim($main_slot_2_expl[0], ' ').":00";
$time_slot_4 = trim($main_slot_2_expl[1], ' ').":00";
}