我正在尝试将来自 API 的 2 个 JSON 对象与 2 个不同的 id 键结合起来。我已经根据我在 react native(javascript) 中的工作代码在 php 中尝试了一些代码,但是重建代码没有按预期工作。
示例第一个 JSON:
[{
poiNumberWinter: null,
minAge: null,
maxAge: null,
minSize: 100,
maxSize: null,
minSizeEscort: 1,
id: 1,
titleImageId: 390,
titleImageWinterId: null,
createdAt: "2018-03-14T15:45:47.000Z",
updatedAt: "2019-03-26T10:58:44.000Z",
_titleWinter: null
}]
示例第二个 JSON:
[{
open: false,
poiId: "1",
closing: null,
opening: "2019-08-15 10:00:00",
showTimes: null,
waitTime: null,
updatedAt: "2019-08-15T20:12:40.000Z",
}]
这是我尝试过的:
$rideTimesconverted = json_decode($rideTimes);
$metaDataConverted = json_decode($metaData);
$fullRideData = array();
for ($i = 0; $i < count($rideTimesconverted); $i++) {
$test = array_filter($metaDataConverted, function ($item) use (
$i,
$rideTimesconverted
) {
return $item['id'] === (int) $rideTimesconverted[$i]['poiId'];
});
array_push($fullRideData, $rideTimesconverted[$i], $test);
}
基于我完美运行的 React Native 代码:
let rideTimes = await GetApiData.getRidesTime();
let rideMetaData = await GetApiData.getRidesMetaData();
let fullRideData = [];
for (let i = 0; i < this.state.rideTimes.length; i++) {
fullRideData.push({
...this.state.rideTimes[i],
...this.state.rideMetaData.find(
itmInner =>
itmInner.id ===
parseInt(this.state.rideTimes[i].poiId)
)
});
}
但结果混淆起来真的很奇怪:
{
open: false,
poiId: "1",
closing: null,
opening: "2019-08-15 10:00:00",
showTimes: null,
waitTime: null,
createdAt: "2019-08-15T22:18:03.000Z",
updatedRow: "2019-08-15T22:18:03.000Z"
},
收到一只叮咚