根据共享值将 3D 数组与 2D 数组合并

发布于 2024-12-11 10:37:54 字数 2457 浏览 0 评论 0原文

我需要根据“id”的共享值将三维数组与二维数组合并。

在下面的示例中,“George Washington”的“id”为 1。我需要附加他的“值”,该值可在第二个数组中找到,其中“id”也是 1。

“John Adams”的“id”是 2,在第二个数组中找不到。因此,他的“值”需要设置为 0(或 NULL)。

最终结果是一个三维数组,其中“值”已添加到原始数组中的每个项目。

数组#1

Array
(
     [0] => Array
         (
             [0] => Array
                 (
                     [id] => 1
                     [name] => "George Washington"
                 )
             [total] => 8
             [average] => 2.5
         )
     [1] => Array
         (
             [0] => Array
                 (
                     [id] => 2
                     [name] => "John Adams"
                 )
             [total] => 6
             [average] => 3.0
         )
     [2] => Array
         (
             [0] => Array
                 (
                     [id] => 5
                     [name] => "James Monroe"
                 )
             [total] => 9
             [average] => 2.0
         )
)

数组#2

Array
(
     [0] => Array
         (
             [id] => 1
             [value] => 12
         )
     [1] => Array
         (
             [id] => 5
             [value] => 18
         )
)

期望的结果:

Array
(
     [0] => Array
         (
             [0] => Array
                 (
                     [id] => 1
                     [name] => "George Washington"
                 )
             [total] => 8
             [average] => 2.5
             [value] => 12
         )
     [1] => Array
         (
             [0] => Array
                 (
                     [id] => 2
                     [name] => "John Adams"
                 )
             [total] => 6
             [average] => 3.0
             [value] => 0
         )
     [2] => Array
         (
             [0] => Array
                 (
                     [id] => 5
                     [name] => "James Monroe"
                 )
             [total] => 9
             [average] => 2.0
             [value] => 18
         )
 )

到目前为止我尝试过的:

我将所有将第一个数组中的“id”值放入名为 $ids 的新数组中。然后,在循环第二个数组中的项目时,我检查是否在 $ids 数组中找到第二个数组中的“id”。

但后来我陷入困境,因为我不知道如何指定第一个数组中的哪个项目需要接收新的“值”。另外,这似乎是一个混乱的解决方案,因为创建 $ids 数组会产生开销。

I need to merge a three-dimensional array with a two-dimensional array based on a shared value for 'id.'

In the example below, "George Washington" has an 'id' of 1. I need to append his 'value', which is found in the second array where 'id' is also 1.

The 'id' of "John Adams" is 2, which is not found in the second array. As a result, his 'value' needs to be set to 0 (or NULL).

The final result is a three-dimension array where 'value' has been added to each item in the original array.

Array #1

Array
(
     [0] => Array
         (
             [0] => Array
                 (
                     [id] => 1
                     [name] => "George Washington"
                 )
             [total] => 8
             [average] => 2.5
         )
     [1] => Array
         (
             [0] => Array
                 (
                     [id] => 2
                     [name] => "John Adams"
                 )
             [total] => 6
             [average] => 3.0
         )
     [2] => Array
         (
             [0] => Array
                 (
                     [id] => 5
                     [name] => "James Monroe"
                 )
             [total] => 9
             [average] => 2.0
         )
)

Array #2

Array
(
     [0] => Array
         (
             [id] => 1
             [value] => 12
         )
     [1] => Array
         (
             [id] => 5
             [value] => 18
         )
)

Desired Result:

Array
(
     [0] => Array
         (
             [0] => Array
                 (
                     [id] => 1
                     [name] => "George Washington"
                 )
             [total] => 8
             [average] => 2.5
             [value] => 12
         )
     [1] => Array
         (
             [0] => Array
                 (
                     [id] => 2
                     [name] => "John Adams"
                 )
             [total] => 6
             [average] => 3.0
             [value] => 0
         )
     [2] => Array
         (
             [0] => Array
                 (
                     [id] => 5
                     [name] => "James Monroe"
                 )
             [total] => 9
             [average] => 2.0
             [value] => 18
         )
 )

What I've tried so far:

I separated all of the 'id' values from the first array into a new array named $ids. Then while looping through the items in the second array, I check to see whether the 'id' from the second array is found in the $ids array.

But then I'm stuck because I don't know how to specify which item in the first array needs to receive the new 'value'. Also, this seems like a messy solution because of the overhead involved in creating the $ids array.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乄_柒ぐ汐 2024-12-18 10:37:54

在我写完这篇文章之前,我什至没有意识到你已经尝试列出 ID 等,但无论如何,你还是可以的。 祝你好运!

$array1_size = count($array1);
$array2_size = count($array2);

// Creates a list containing all available IDs of Array1
for ($i = 0; $i < $array1_size; $i ++) {
    $id_list[] = $array1[$i][0]['id'];
    $array1[$i]['value'] = NULL; // Adds a NULL to all value fields
}

// Loops through Array2
for ($i = 0; $i < $array2_size; $i++) {
    if (in_array($array2[$i]['id'], $id_list)) { // Checks if each ID exists in the ID list

        $key = array_search($array2[$i]['id'], $id_list); // Gets the key of the matching ID
        $array1[$key]['value'] = $array2[$i]['value']; // Adds the value

    }
}

编辑:现在默认值设置为 NULL,因此仅在以后需要时才进行更改。

Didn't even realize you had tried to list the IDs and such before I was done writing this, but here you go anyway. Good luck!

$array1_size = count($array1);
$array2_size = count($array2);

// Creates a list containing all available IDs of Array1
for ($i = 0; $i < $array1_size; $i ++) {
    $id_list[] = $array1[$i][0]['id'];
    $array1[$i]['value'] = NULL; // Adds a NULL to all value fields
}

// Loops through Array2
for ($i = 0; $i < $array2_size; $i++) {
    if (in_array($array2[$i]['id'], $id_list)) { // Checks if each ID exists in the ID list

        $key = array_search($array2[$i]['id'], $id_list); // Gets the key of the matching ID
        $array1[$key]['value'] = $array2[$i]['value']; // Adds the value

    }
}

Edit: Values are now set to NULL by default and thus only gets changed later on if needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文