简体   繁体   English

如何在 PHP 数组中找到所有结果,合并值,然后合并到另一个数组中

[英]How can I find all results in a PHP array, merge values, and then merge into another array

Using PHP, I have an array like this:使用 PHP,我有一个这样的数组:

Array 1数组 1

[
  {epid: "123", hash: "xxxxxx"},
  {epid: "456", hash: "xxxxxx"},
  {epid: "789", hash: "xxxxxx"},
  {epid: "123", hash: "xxxxxx"},
  {epid: "123", hash: "xxxxxx"},
]

Then, I have a second array like this:然后,我有第二个这样的数组:

Array 2阵列 2

[
  {epid: "123", name: "This is a title"},
  {epid: "456", name: "This is a title"},
  {epid: "789", name: "This is a title"}
]

My goal is to get all hash from array one and add them to the appropriate record in array 2. From this example, the results would be:我的目标是从数组 1 中获取所有散列并将它们添加到数组 2 中的相应记录中。从这个例子中,结果将是:

[
  {epid: "123", name: "This is a title", hash: [ xxxxxx, xxxxxx, xxxxxx ] },
  {epid: "456", name: "This is a title", hash: [ xxxxxx ] },
  {epid: "789", name: "This is a title", hash: [ xxxxxx ] }
]

I'm sure there are multiple loops here, but for the life of me, I can't wrap my brain around it.我确信这里有多个循环,但对于我的生活,我无法将我的大脑环绕在它周围。

You could loop through the second array and use the epid to find the indexes in the first array.您可以遍历第二个数组并使用 epid 查找第一个数组中的索引。 Then for every index found, add the hash to the current loop item:然后对于找到的每个索引,将哈希添加到当前循环项:

$lookup = [
    ["epid" => "123", "hash" => "xxxxxxA"],
    ["epid" => "456", "hash" => "xxxxxxB"],
    ["epid" => "789", "hash" => "xxxxxxC"],
    ["epid" => "123", "hash" => "xxxxxxD"],
    ["epid" => "123", "hash" => "xxxxxxE"],
];

$db = [
    ["epid" => "123", "name" => "This is a title"],
    ["epid" => "456", "name" => "This is a title"],
    ["epid" => "789", "name" => "This is a title"]
];

foreach($db as $i => $el) {
    $keys = array_keys(array_column($lookup, 'epid'), $el["epid"]);
    foreach($keys as $key) {
        $db[$i]["hash"][] = $lookup[$key]["hash"];
    }
}

var_dump($db);

I assume, u don't actually have json array, but php array.我假设,你实际上没有 json 数组,而是 php 数组。 If not you have to convert them before.如果不是,您必须先转换它们。 Iterate over each entry in array2 and filter the matching items out of the array1.迭代 array2 中的每个条目并过滤 array1 中的匹配项。 If done, you can easily get the hashes via array_column and add them to array2.如果完成,您可以通过array_column轻松获取哈希并将它们添加到 array2。

$array1 = [
  ['epid' => "123", 'hash' => "xxxxxx"],
  ['epid' => "456", 'hash' => "xxxxxx"],
  ['epid' => "789", 'hash' => "xxxxxx"],
  ['epid' => "123", 'hash' => "xxxxxx"],
  ['epid' => "123", 'hash' => "xxxxxx"],
];

$array2 = [
  ['epid' => "123", 'name' => "This is a title"],
  ['epid' => "456", 'name' => "This is a title"],
  ['epid' => "789", 'name' => "This is a title"]
];

foreach ($array2 as $key => $data) {
    $matching = array_filter($array1, static fn($filterValue) => $data['epid'] === $filterValue['epid']);
    $array2[$key]['hash'] = array_column($matching, 'hash');
}

Or, you could do it as short as possible with the following statement.或者,您可以使用以下语句尽可能简短。 It does exactly the same as the above, but is way more unreadable.它与上面的完全相同,但更不可读。

array_walk($array2, static fn(&$value) => $value['hash'] = array_column(array_filter($array1, static fn($filterValue) => $filterValue['epid'] === $value['epid']), 'hash'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM