【问题标题】:How to merge 2 array based on value?如何根据值合并 2 个数组?
【发布时间】:2019-12-30 13:27:21
【问题描述】:

我想将两个数组合二为一。这里我有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
  }
]

说明

在输出数组中,数组的key是remarks数组的label。该键的 是在所述asset 数组中找到的value 键的数据。加入者是assets.remarks_id == remarks.id

所以显示的键是动态的。我怎样才能以这种方式合并两个数组?所以值部分必须与备注的id匹配,例如 资产value"Txt Def": 并且具有与id 相同的remarks_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

【问题讨论】:

  • 好的。全清。到目前为止,您是否尝试过任何不起作用的方法?我们很乐意为您提供帮助。但是没有人(希望)会为您编写完整的代码。
  • 等等,我会编辑我的帖子
  • 输出不应该有两个对象而不是一个吗?一个asset_id?

标签: php arrays laravel-5


【解决方案1】:

您可以先创建一个以备注 ID 为键的映射(关联数组),这样您就可以通过该 ID 进行快速查找。

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

然后访问所有资产记录,通过remark 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

【讨论】:

    猜你喜欢
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    • 2016-11-17
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多