如何基于值合并2个数组?

时间:2019-12-30 13:27:21

标签: php arrays laravel-5

我想将两个数组合并为一个。这里我有2个数组 remarksremark_asset

这是备注

的数组
[
  {
    "id": 5,
    "type": "Text",
    "name": "Text",
    "label": "Txt L",
    "description": "Txt D",
    "filter_logic": null,
    "default": "Txt Def",
    "weight": "12",
    "required": "true",
    "created_at": "2019-12-10 18:20:37",
    "updated_at": "2019-12-10 18:20:37"
  },
  {
    "id": 6,
    "type": "Date",
    "name": "Date",
    "label": "Date Label",
    "description": "Date DEsc",
    "filter_logic": null,
    "default": "2019-12-10",
    "weight": "12",
    "required": "false",
    "created_at": "2019-12-10 18:30:29",
    "updated_at": "2019-12-10 18:30:29"
  },
  {
    "id": 7,
    "type": "Checkbox",
    "name": "Kotaro",
    "label": "Cex",
    "description": "cex desc",
    "filter_logic": null,
    "default": "true",
    "weight": "11",
    "required": "false",
    "created_at": "2019-12-10 18:32:13",
    "updated_at": "2019-12-10 18:32:13"
  },
  {
    "id": 13,
    "type": "List",
    "name": null,
    "label": "Label",
    "description": "Desc",
    "filter_logic": null,
    "default": "1,2,3,4",
    "weight": null,
    "required": "false",
    "created_at": "2019-12-30 01:46:44",
    "updated_at": "2019-12-30 01:46:44"
  }
]

这是一组备注资产

[
  [
    {
      "id": 210,
      "asset_id": 94092,
      "remark_id": 5,
      "value": "Txt Def",
      "created_at": "2019-12-13 08:55:41",
      "updated_at": "2019-12-13 08:55:41"
    },
    {
      "id": 211,
      "asset_id": 94092,
      "remark_id": 6,
      "value": "2019-12-10",
      "created_at": "2019-12-13 08:55:41",
      "updated_at": "2019-12-13 08:55:41"
    },
    {
      "id": 212,
      "asset_id": 94092,
      "remark_id": 7,
      "value": "true",
      "created_at": "2019-12-13 08:55:41",
      "updated_at": "2019-12-13 08:55:41"
    }
  ],
  [
    {
      "id": 213,
      "asset_id": 94093,
      "remark_id": 5,
      "value": "Txt Def",
      "created_at": "2019-12-13 09:00:16",
      "updated_at": "2019-12-13 09:00:16"
    },
    {
      "id": 214,
      "asset_id": 94093,
      "remark_id": 6,
      "value": "2019-12-10",
      "created_at": "2019-12-13 09:00:16",
      "updated_at": "2019-12-13 09:00:16"
    },
    {
      "id": 215,
      "asset_id": 94093,
      "remark_id": 7,
      "value": "true",
      "created_at": "2019-12-13 09:00:16",
      "updated_at": "2019-12-13 09:00:16"
    }
  ]
]

我的预期输出是这样的:

[
 {
    "Txt L": "Txt Def",
    "Date Label": "2019-12-10",
    "Cex": "true",
    "Label": null
  }
]

说明

在输出数组中,该数组的键是label数组的remarks。该键的 value 是在上述value数组中找到的asset键的数据。连接由assets.remarks_id == remarks.id

进行

因此显示的键将是动态的。如何以这种方式合并两个数组?因此,值部分必须与备注的id相匹配,例如 资产value"Txt Def":,并且与remarks_id具有相同的id,其中label"Txt L":

这是我的代码:

$getData = $data->get()->take(10);
$remark = Remark::all();
$remarkAsset = RemarkAsset::all();

/* ------------------------------ REMARK ASSET ------------------------------ */
$resultRemarkAsset = array();
foreach ($remarkAsset as $dataRemark) {
    $resultRemarkAsset[$dataRemark['asset_id']][] = $dataRemark;
}
$remarkAssetValue = array_values($resultRemarkAsset); 

//result

1 个答案:

答案 0 :(得分:0)

您可以首先创建一个以备注ID为键的地图(关联数组),以便可以通过该ID快速查找。

同时初始化结果对象,因此它具有所有标签作为键(以null作为值)

然后访问所有资产记录,并按备注ID在上面的地图中找到条目。使用找到的标签更新结果对象。

我已将两个输入数组命名为$remarks$assets

// Create a map keyed by remark id, and create a template object:
$template = [];
foreach($remarks as $remark) {
    $map[$remark['id']] = $remark;
    $template[$remark['label']] = null;
}

$results = [];
foreach($assets as $assetArray) {
    $result = $template; // copy the template
    // For each asset entry find the label via the map:
    foreach($assetArray as $asset) {
        $id = $asset['remark_id'];
        if (isset($map[$id])) {
            $remark = $map[$id];
            $result[$remark['label']] = $asset['value'];
        }
    }
    $results[] = $result;
}

运行此操作后,$results将具有您提供的示例数据的以下内容:

[{
   "Txt L":"Txt Def",
   "Date Label":"2019-12-10",
   "Cex":"true",
   "Label":null
},{
   "Txt L":"Txt Def",
   "Date Label":"2019-12-10",
   "Cex":"true",
   "Label":null
}]

它有两个对象,$assets变量中的每个子数组一个。如果只需要一个对象,则只需更改最终循环,即可将所有内容放入同一$result对象中:

$results = [];
$result = $template; // copy the template before the loop
foreach($assets as $assetArray) {
    // For each asset entry find the label via the map:
    foreach($assetArray as $asset) {
        $id = $asset['remark_id'];
        if (isset($map[$id])) {
            $remark = $map[$id];
            $result[$remark['label']] = $asset['value'];
        }
    }
}
$results[] = $result; // There will now only be one entry in the output