-2

I have multiple arrays of objects, and I'm looking for a way to merge them into a single array. Each array represents a set of ratings for different companies, and I want to combine these arrays to create a single array that contains all the ratings. Here's an example of the arrays I have:

Array 1:

Array
(
    [0] => stdClass Object
        (
            [id] => 3
            [ratingdet_id] => 2
            [first_name] => Muthupandi
            [company_name] => Thangam Pazhamudhir Solai - Neelampur - CBE
            [ratings_name] => Store Response
            [rating_val] => 3.5
            [createdate] => 2023-08-31 13:18:12
        )

    [1] => stdClass Object
        (
            [id] => 4
            [ratingdet_id] => 2
            [first_name] => Muthupandi
            [company_name] => Thangam Pazhamudhir Solai - Neelampur - CBE
            [ratings_name] => Waiting Time
            [rating_val] => 4
            [createdate] => 2023-08-31 13:18:12
        )

)

Array 2:

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [ratingdet_id] => 1
            [first_name] => Muthupandi
            [company_name] => Dataprime Technologies- CBE 
            [ratings_name] => Store Response
            [rating_val] => 2.5
            [createdate] => 2023-08-31 13:16:12
        )

    [1] => stdClass Object
        (
            [id] => 2
            [ratingdet_id] => 1
            [first_name] => Muthupandi
            [company_name] => Dataprime Technologies- CBE 
            [ratings_name] => Waiting Time
            [rating_val] => 4.5
            [createdate] => 2023-08-31 13:16:12
        )

)

I expect to merge these arrays into a single array that looks like this:

Merged Array:

Array
(
    [0] => stdClass Object
        (
            [id] => 3
            [ratingdet_id] => 2
            [first_name] => Muthupandi
            [company_name] => Thangam Pazhamudhir Solai - Neelampur - CBE
            [ratings_name] => Store Response
            [rating_val] => 3.5
            [createdate] => 2023-08-31 13:18:12
        )

    [1] => stdClass Object
        (
            [id] => 4
            [ratingdet_id] => 2
            [first_name] => Muthupandi
            [company_name] => Thangam Pazhamudhir Solai - Neelampur - CBE
            [ratings_name] => Waiting Time
            [rating_val] => 4
            [createdate] => 2023-08-31 13:18:12
        )

    [2] => stdClass Object
        (
            [id] => 1
            [ratingdet_id] => 1
            [first_name] => Muthupandi
            [company_name] => Dataprime Technologies- CBE 
            [ratings_name] => Store Response
            [rating_val] => 2.5
            [createdate] => 2023-08-31 13:16:12
        )

    [3] => stdClass Object
        (
            [id] => 2
            [ratingdet_id] => 1
            [first_name] => Muthupandi
            [company_name] => Dataprime Technologies- CBE 
            [ratings_name] => Waiting Time
            [rating_val] => 4.5
            [createdate] => 2023-08-31 13:16:12
        )

)

Can someone please guide me on how to achieve this in PHP? Should I use a loop or is there a built-in function that can help me achieve this efficiently? I want to make sure that the resulting merged array maintains the original order of objects from each source array. Any help or insights would be greatly appreciated.

qɐʇǝɥɐW
  • 347
  • 3
  • 17
  • Use array_merge() function, merges one or more arrays into one array. – Mukhila Asokan Sep 01 '23 at 11:40
  • Please share more details, like the code involved. Are you sure this problem is related to MySQL or CodeIgniter in any way? – Nico Haase Sep 01 '23 at 11:51
  • What code did you try to solve this? – Teun Sep 01 '23 at 11:52
  • Take the [tour] and read [ask], please. There is no question, problem statement, or relevant code here. We can _help_ you with a _specific_ issue within _your_ attempt to research, design and implement a solution to your requirement. But we're not just a free do-my-thinking or write-my-code service where you can just dump any requirements you don't understand or don't want to spend time on, expecting someone to just resolve it all for you without any time or money spent by you. Why do you imagine the volunteers here would find that satisfying? If you want a no-effort answer, hire a freelancer. – ADyson Sep 01 '23 at 12:06
  • 1
    _"i expected"_ - based on _what_ ...? This lacks _any_ explanation of where that data is coming from, or what you are doing to it. – CBroe Sep 01 '23 at 13:25

1 Answers1

-1
$mergedArray = array_merge($array1, $array2);

print_r($mergedArray);
qɐʇǝɥɐW
  • 347
  • 3
  • 17