如果某些索引匹配php

时间:2018-09-02 07:23:21

标签: php arrays

我在php中有两个数组,我想做的是如果drug_ids匹配,我想将第二个数组添加为子数组。 我有什么

Array
(
    [0] => Array
        (
            [drug_id] => 1
            [drug] => Abacavir 300mg - Tabs
        )

    [1] => Array
        (
            [drug_id] => 4
            [drug] => Abacavir/Lamivudine 120/60mg - FDC Tabs
        )

    [2] => Array
        (
            [drug_id] => 3
            [drug] => Abacavir/Lamivudine 600/300mg - FDC Tabs
        )
);

具有更多数据的第二个数组

Array
(
    [0] => Array
        (
            [id] => 2
            [decision_date] => 2018-08-10
            [discussion] => The product is at 2.6 MOS
            [recommendation] => recommendation
            [drug_id] => 1
            [created] => 2018-08-16 09:23:09
            [user] => System Admin
        )

    [1] => Array
        (
            [id] => 3
            [decision_date] => 2018-08-10
            [discussion] => recommendation.
            [recommendation] =>recommendation.
            [drug_id] => 4
            [created] => 2018-08-16 09:23:09
            [user] => System Admin
        )

    [2] => Array
        (
            [id] => 4
            [decision_date] => 2018-08-10
            [discussion] => The product is at 6MOS.
            [recommendation] =>ggfgfg.
            [drug_id] => 4
            [created] => 2018-08-16 09:23:09
            [user] => System Admin
        )
);

如果药物ID匹配以创建最终阵列,我想将第二个阵列推入第一个阵列作为决定

 [0] => Array
        (
            [drug_id] => 4
            [drug] => Abacavir/Lamivudine 120/60mg - FDC Tabs
            [decisions] => Array(
                  [0] => Array
                            (
                             [id] => 3
                             [decision_date] => 2018-08-10
                             [discussion] => recommendation.
                             [recommendation] =>recommendation.
                             [drug_id] => 4
                             [created] => 2018-08-16 09:23:09
                             [user] => System Admin
                            )

                  [1] => Array
                            (
                             [id] => 4
                             [decision_date] => 2018-08-10
                             [discussion] => The product is at 6MOS.
                             [recommendation] =>ggfgfg.
                             [drug_id] => 4
                             [created] => 2018-08-16 09:23:09
                             [user] => System Admin
                            )

            )
        )

关于如何实现此目标的任何建议?

我尝试过的    $ table_data //第一个数组     $ table_data //第二个数组

foreach ($table_data as $mt) {
                foreach ($items as $it) {
                    if ($it['drug_id'] == $mt['drug_id']) {
                        $decision['decisions'] = $it;
                        array_push($table_data, $decision);
                    }
                }
            }

//这不起作用

2 个答案:

答案 0 :(得分:2)

有一个错误。尝试以下

df1 = pd.Series(df.columns).reset_index()
df1.columns = ['Index_Number','index_Name']

答案 1 :(得分:0)

另一种方式:

foreach ($arr1 as &$a) {
    $a["decisions"] = array_filter($arr2, function ($b) use ($a) {
        return $b["drug_id"] === $a["drug_id"];
    });
}