按特定字段合并两个数组

时间:2017-07-17 13:13:24

标签: php arrays

我有一个看起来像的数组 $ firstArray

[0] => Array(
   [ID] => 1
   [Fruit] => Apple
   [State] => Ohio
    )
 [1] => Array(
   [ID] => 2
   [Fruit] => Orange
   [State] => Hawaii
    )
 [2] => Array(
   [ID] => 3
   [Fruit] => Orange
   [State] => Vermont
    )

另一个看起来像的数组 $ secondArray

 [0] => Array(
   [ID] => 1
   [description] => This is sample description
   [Price] => 20
    )
 [1] => Array(
   [ID] => 1
   [Fruit] => This is sample description 2
   [Price] => 15
    )
 [2] => Array(
   [ID] => 2
   [Fruit] => This is the second description
   [Price] => 100
    )
 [3] => Array(
   [ID] => 2
   [Fruit] => This is the second description 2
   [Price] => 50
    )
 [4] => Array(
   [ID] => 3
   [Fruit] => This is the third description
   [Price] => 99
    )

我用过

$newArray = array_merge_recursive($firstArray, $secondArray);

这只是从第二个数组到第一个数组末尾的附加条目,理想情况下我希望新数组看起来像这样

 [0] => Array(
   [ID] => 1
   [Fruit] => Apple
   [State] => Ohio
   [description]
       Array(
         [0] => This is sample description
         [1] => This is sample description 2
    )
   [price]
       Array(
         [0] => 20
         [1] => 15
    )
 [1] => Array(
   [ID] => 1
   [Fruit] => Apple
   [State] => Ohio
   [description]
       Array(
         [0] => This is the second description
         [1] => This is the second description 2
    )
   [price]
       Array(
         [0] => 100
         [1] => 50
    )

本质上,新数组应该在ID上组合,并从第二个数组创建嵌套数组。

1 个答案:

答案 0 :(得分:1)

看看这是否有意义。 https://iconoun.com/demo/temp_jumpman.php



<?php // demo/temp_jumpman.php
/**
 * Dealing with PHP nested arrays
 *
 * https://stackoverflow.com/questions/45145282/merge-two-arrays-by-specific-fields
 */
error_reporting(E_ALL);
echo '<pre>';

// TEST DATA CREATED FROM THE POST AT STACK - ID, Fruit, State
$first = Array(
 '0' => [
   'ID' => '1',
   'Fruit' => 'Apple',
   'State' => 'Ohio',
    ],
 '1' => [
   'ID' => '2',
   'Fruit' => 'Orange',
   'State' => 'Hawaii',
    ],
 '2' => [
   'ID' => '3',
   'Fruit' => 'Orange',
   'State' => 'Vermont',
    ],
);

// TEST DATA CREATED FROM THE POST AT STACK - ID, description, Price
$second = Array(
 '0' => [
   'ID' => '1',
   'description' => 'This is sample description',
   'Price' => '20',
    ],
 '1' => [
   'ID' => '1',
   'description' => 'This is sample description 2',
   'Price' => '15',
    ],
 '2' => [
   'ID' => '2',
   'description' => 'This is the second description',
   'Price' => '100',
    ],
 '3' => [
   'ID' => '2',
   'description' => 'This is the second description 2',
   'Price' => '50',
    ],
 '4' => [
   'ID' => '3',
   'description' => 'This is the third description',
   'Price' => '99',
    ],
);

$out = [];
foreach ($first as $arr)
{
    // PLACEHOLDERS FOR THE FOUND DATA FROM THE SECOND ARRAY
    $arr['description'] = [];
    $arr['Price'] = [];
    $id = $arr['ID']; // THE ID WE WANT IN THE SECOND ARRAY
    foreach ($second as $idp)
    {
        if ($idp['ID'] == $arr['ID'])
        {
            $arr['description'][] = $idp['description'];
            $arr['Price'][] = $idp['Price'];
        }
    }
    $out[] = $arr;
}
print_r($out);
&#13;
&#13;
&#13;