3

The number of items in both arrays $newResponse & $key will always be same.For each item of the $newResponse, i wanted to add items of $key to $newResponse in the same order they exists in $key array as mentioned in the Expected result.How can i achieve that?

dump($newResponse); is an array like below,which am getting as the result of a foreach loop.

array:4 [
  0 => array:6 [
    "courseId" => 18
    "courseDisplayName" => "qqq"
  ]
  1 => array:6 [
    "courseId" => 1
    "courseDisplayName" => "ips"
  ]
  2 => array:6 [
    "courseId" => 18
    "courseDisplayName" => "qqq"
  ]
  3 => array:6 [
    "courseId" => 1
    "courseDisplayName" => "ips"
  ]
]

dump($key); is an array like below, which is the result of another foreach loop.

array:4[
    0=>[
      "totalPoints" => 2
      "percent" => 1.0
      "id" => 2
    ]
    1=> [
      "totalPoints" => 10
      "percent" => 2
      "id" => 3
    ]
    2=> [
      "totalPoints" => 4
      "percent" => 0.0
      "id" => 6
    ]
    3=> [
      "totalPoints" => 4
      "percent" => 0.0
      "id" => 5
    ]
   ]

Expected result:

[
      [
        "courseId" => 18
        "courseDisplayName" => "qqq"
        "totalPoints" => 2
        "percent" => 1.0
        "id" => 2
      ]
      [
        "courseId" => 1
        "courseDisplayName" => "ips"
        "totalPoints" => 10
        "percent" => 2
        "id" => 3
      ]
      [
        "courseId" => 18
        "courseDisplayName" => "qqq"
        "totalPoints" => 4
        "percent" => 0.0
        "id" => 6
      ]
      [
        "courseId" => 1
        "courseDisplayName" => "ips"
        "totalPoints" => 4
        "percent" => 0.0
        "id" => 5
      ]
    ]
  • Did you try with a foreach? – nice_dev Jan 05 '22 at 05:07
  • [`This`](https://laravel.com/docs/8.x/collections#method-merge) seems relevant. – nice_dev Jan 05 '22 at 05:09
  • @nice_devi know may be i can work it out with a foreach. But i don't know how to do that? can u show me the code? –  Jan 05 '22 at 05:19
  • @user1597430, i know programming basics, i was asking for help to know the logic of what to work out in the foreach loop here –  Jan 05 '22 at 05:28
  • @nice_devi tried the `merge()` method of collection as u mentioned in the link. i put it inside the 2nd foreach loop i mentioned in orginal question. Then, it merges the 2nd array with only the first of of 1st array, not with all item of the 1st array in order. –  Jan 05 '22 at 05:44
  • This is a mega-duplicate question which I will hammering closed after a complete a comprehensive sweep of all duplicate pages on this topic. In the meantime, this is most simply achieved with: [`array_map('array_merge', $newResponse, $key)`](https://3v4l.org/XYd9j) – mickmackusa Oct 07 '22 at 04:53

1 Answers1

3

Here's one way to do it:

Assuming your data is:

        $newResponse = [
            [
                "courseId" => 18,
                "courseDisplayName" => "qqq"
            ],
            [
                "courseId" => 1,
                "courseDisplayName" => "ips",
            ],
            [
                "courseId" => 18,
                "courseDisplayName" => "qqq",
            ],
            [
                "courseId" => 1,
                "courseDisplayName" => "ips",
            ]
        ];

        $key = [
            [
        "totalPoints" => 2,
        "percent" => 1.0,
        "id" => 2
      ],
      [
        "totalPoints" => 10,
        "percent" => 2,
        "id" => 3
      ],
      [
        "totalPoints" => 4,
        "percent" => 0.0,
        "id" => 6
      ],
      [
        "totalPoints" => 4,
        "percent" => 0.0,
        "id" => 5
      ]
    ];

Then I'd probably reach for array_map, having used it for this kind of purpose recently:

        $out = [];
        array_map(function($a,$b) use (&$out) {
            $out[] = $a + $b;
        },$newResponse,$key);
        print_r($out);

Note in the above the + is very similar to array merge and I believe interchangeable in your use case. You can read about the difference here: Array_merge versus +

Kevin Y
  • 646
  • 5
  • 18