在 PHP 中通过不同的键将 2 个 JSON 字符串合并为一个

我正在尝试将来自 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"

},


MMTTMM
浏览 182回答 1
1回答

收到一只叮咚

如果我正确阅读了您的问题,此功能应该可以完成工作。$rideTimes = '[{&nbsp; &nbsp; "poiNumberWinter": null,&nbsp; &nbsp; "minAge": null,&nbsp; &nbsp; "maxAge": null,&nbsp; &nbsp; "minSize": 100,&nbsp; &nbsp; "maxSize": null,&nbsp; &nbsp; "minSizeEscort": 1,&nbsp; &nbsp; "id": 1,&nbsp; &nbsp; "titleImageId": 390,&nbsp; &nbsp; "titleImageWinterId": null,&nbsp; &nbsp; "createdAt": "2018-03-14T15:45:47.000Z",&nbsp; &nbsp; "updatedAt": "2019-03-26T10:58:44.000Z",&nbsp; &nbsp; "_titleWinter": null},{&nbsp; &nbsp; "poiNumberWinter": null,&nbsp; &nbsp; "minAge": null,&nbsp; &nbsp; "maxAge": null,&nbsp; &nbsp; "minSize": 100,&nbsp; &nbsp; "maxSize": null,&nbsp; &nbsp; "minSizeEscort": 1,&nbsp; &nbsp; "id": 3,&nbsp; &nbsp; "titleImageId": 390,&nbsp; &nbsp; "titleImageWinterId": null,&nbsp; &nbsp; "createdAt": "2018-03-14T15:45:47.000Z",&nbsp; &nbsp; "updatedAt": "2019-03-26T10:58:44.000Z",&nbsp; &nbsp; "_titleWinter": null}]';$metaData = '[{&nbsp; &nbsp; "open": false,&nbsp; &nbsp; "poiId": "1",&nbsp; &nbsp; "closing": null,&nbsp; &nbsp; "opening": "2019-08-15 10:00:00",&nbsp; &nbsp; "showTimes": null,&nbsp; &nbsp; "waitTime": null,&nbsp; &nbsp; "updatedAt": "2019-08-15T20:12:40.000Z"},{&nbsp; &nbsp; "open": false,&nbsp; &nbsp; "poiId": "2",&nbsp; &nbsp; "closing": null,&nbsp; &nbsp; "opening": "2019-08-15 15:00:00",&nbsp; &nbsp; "showTimes": null,&nbsp; &nbsp; "waitTime": null,&nbsp; &nbsp; "updatedAt": "2019-08-15T20:12:40.000Z"}]';function someFancyName($arrRideTime, $arrMetaData){&nbsp; &nbsp; $hits = [];&nbsp; &nbsp; foreach ($arrRideTime as $rTime) {&nbsp; &nbsp; &nbsp; &nbsp; foreach ($arrMetaData as $mData) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($mData['poiId'] == $rTime['id']) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $hits[] = array_merge($rTime, $mData);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $hits;}$arrRideTime = json_decode($rideTimes, true);$arrMetaData = json_decode($metaData, true);echo '<pre><code>';var_dump(someFancyName($arrRideTime, $arrMetaData));echo '</code></pre>';
打开App,查看更多内容
随时随地看视频慕课网APP