0

I have an two arrays in php, what i would like to do is if drug_ids match, I want to add the second array as a sub array. What I have

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
        )
);

The second array with more data

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
        )
);

I would like to push the second array into the first one as decisions if drug ids match to create a final array

 [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
                            )

            )
        )

Any suggestions as to how this can be achieved?

What i have tried $table_data //first array $table_data //second array

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

//This does not work

3
  • 1
    Have you tried anything so far? Using foreach() loops and matching the two sets of values together? Commented Sep 2, 2018 at 7:35
  • This is very common task. Try something and then, if you have any issues, we'll help you to resolve them Commented Sep 2, 2018 at 7:39
  • @NikitaLeshchev and Ren please see the edit of what I have tried Commented Sep 2, 2018 at 7:41

3 Answers 3

2

There's a bug. Try the following

foreach ($table_data as &$mt) {
    foreach ($items as $it) {
        if ($it['drug_id'] == $mt['drug_id']) {
            if (!isset($mt['decisions'])) {
                $mt['decisions'] = [];
            }
            $mt['decisions'][] = $it;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Another way:

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

Comments

0

I do not recommended nested loops they will do too many unnecessary iterations. Instead, because the drug_ids in the first array are unique, copy them to be the first level's keys -- this will let you swiftly associate data between the two arrays. When the loop is done, you can remove the first level keys with array_values().

Code: (Demo)

$drugs = array_column($array1, null, 'drug_id');
foreach ($array2 as $row) {
    if (isset($drugs[$row['drug_id']])) {
        $drugs[$row['drug_id']]['decisions'][] = $row;
    }
}
var_export(array_values($drugs));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.