现在我有两个阵列:
first one: "workingHours": [ { "opening": "09:30", "closing": "13:30", "dayName": "sunday" }, { "opening": "", "closing": "", "dayName": "monday" }, { "opening": "", "closing": "", "dayName": "tuesday" }, { "opening": "10:30", "closing": "06:30", "dayName": "wednesday" }, { "opening": "01:00", "closing": "08:00", "dayName": "thursday" }, { "opening": "", "closing": "", "dayName": "friday" }, { "opening": "", "closing": "", "dayName": "saturday" }, ],
Second one: "discount": [ { "from_time": "13:00:00", "to_time": "14:50:00", "discount": "20", "dayName": "wednesday" }, { "from_time": "06:13:16", "to_time": "04:14:11", "discount": "20", "dayName": "monday" }, { "from_time": "05:10:15", "to_time": "06:10:17", "discount": "20", "dayName": "tuesday" }, { "from_time": "06:59:16", "to_time": "04:19:11", "discount": "20", "dayName": "monday" } ],
现在我想要这样的输出:
Wanted Output: "workingHours": { "opening": "", "closing": "", "dayName": "monday" { "from_time": "06:13:16", "to_time": "04:14:11", "discount": "20", "dayName": "monday" }, { "from_time": "06:59:16", "to_time": "04:19:11", "discount": "20", "dayName": "monday" } },
The output that is coming right now: "workingHours": [ { "opening": "09:30", "closing": "13:30", "dayName": "sunday" }, { "0": { "from_time": "06:13:16", "to_time": "04:14:11", "discount": "20", "dayName": "monday" }, "1": { "from_time": "06:59:16", "to_time": "04:19:11", "discount": "20", "dayName": "monday" }, "opening": "", "closing": "", "dayName": "monday" }, { "0": { "from_time": "05:10:15", "to_time": "06:10:17", "discount": "20", "dayName": "tuesday" }, "opening": "", "closing": "", "dayName": "tuesday" }, { "0": { "from_time": "13:00:00", "to_time": "14:50:00", "discount": "20", "dayName": "wednesday" }, "opening": "10:30", "closing": "06:30", "dayName": "wednesday" }, { "opening": "01:00", "closing": "08:00", "dayName": "thursday" }, { "opening": "", "closing": "", "dayName": "friday" }, { "opening": "", "closing": "", "dayName": "saturday" } ],
这是我的代码:
foreach ($returnDataAns['workingHours'] as $key => $value) { # code... foreach ($returnDataAns['discount'] as $key2 => $value2) { # code... if ($value['dayName'] == $value2['dayName']) { # code... array_push($returnDataAns['workingHours'][$key], $value2); } } }
知道我该怎么办?我只想删除那些0:1:键!
答案 0 :(得分:0)
这将为您提供所需的输出......
$arr1 = json_decode('[
{
"opening": "09:30",
"closing": "13:30",
"dayName": "sunday"
},
{
"opening": "",
"closing": "",
"dayName": "monday"
},
{
"opening": "",
"closing": "",
"dayName": "tuesday"
},
{
"opening": "10:30",
"closing": "06:30",
"dayName": "wednesday"
},
{
"opening": "01:00",
"closing": "08:00",
"dayName": "thursday"
},
{
"opening": "",
"closing": "",
"dayName": "friday"
},
{
"opening": "",
"closing": "",
"dayName": "saturday"
}
]', true);
$arr2 = json_decode('[
{
"from_time": "13:00:00",
"to_time": "14:50:00",
"discount": "20",
"dayName": "wednesday"
},
{
"from_time": "06:13:16",
"to_time": "04:14:11",
"discount": "20",
"dayName": "monday"
},
{
"from_time": "05:10:15",
"to_time": "06:10:17",
"discount": "20",
"dayName": "tuesday"
},
{
"from_time": "06:59:16",
"to_time": "04:19:11",
"discount": "20",
"dayName": "monday"
}
]', true);
$finalArray = [];
foreach ($arr1 as $key => $day) {
$subArray = [];
foreach ($arr2 as $key2 => $dayData) {
if ($day['dayName'] === $dayData['dayName']) {
$subArray[] = $dayData;
}
}
$finalArray[$key] = !empty($subArray) ? $finalArray[$key] = [$day, $subArray] : [$day];
}
echo '<pre>';
echo '$finalArray ' . print_r($finalArray, true);
echo '</pre>';
echo '<pre>';
echo '$finalArray json_encoded ' . print_r(json_encode($finalArray), true);
echo '</pre>';
exit;
这将为您提供json_encode
输出中的以下JSON字符串:
[
[
{
"opening":"09:30",
"closing":"13:30",
"dayName":"sunday"
}
],
[
{
"opening":"",
"closing":"",
"dayName":"monday"
},
[
{
"from_time":"06:13:16",
"to_time":"04:14:11",
"discount":"20",
"dayName":"monday"
},
{
"from_time":"06:59:16",
"to_time":"04:19:11",
"discount":"20",
"dayName":"monday"
}
]
],
[
{
"opening":"",
"closing":"",
"dayName":"tuesday"
},
[
{
"from_time":"05:10:15",
"to_time":"06:10:17",
"discount":"20",
"dayName":"tuesday"
}
]
],
[
{
"opening":"10:30",
"closing":"06:30",
"dayName":"wednesday"
},
[
{
"from_time":"13:00:00",
"to_time":"14:50:00",
"discount":"20",
"dayName":"wednesday"
}
]
],
[
{
"opening":"01:00",
"closing":"08:00",
"dayName":"thursday"
}
],
[
{
"opening":"",
"closing":"",
"dayName":"friday"
}
],
[
{
"opening":"",
"closing":"",
"dayName":"saturday"
}
]
]
答案 1 :(得分:0)
我认为这会给你预期的输出 我使用array_combine和array_column来使第一个数组相关联 必须循环第二个数组以创建可与第一个数组合并的数组。
最后,我使用array_values删除关联。
foreach($discount as $val){
$discount2[$val['dayName']][] = $val;
}
$res = array_values(array_merge_recursive(array_combine(array_column($WH, "dayName"), $WH), $discount2));
var_dump($res);