0

Hi I'm trying to fill this array

Array
(
    [0] => Array
        (
            [id] => 107
            [name] => Sara
            [msgSinleer] => 2
            [subject] => Array
                (
                )
        )

    [1] => Array
        (
            [id] => 110
            [name] => Manuela 
            [msgSinleer] => 27
            [subject] => Array
                (
                )
        )

)

with the values of this array if one of the ID match

Array
(
    [0] => Array
        (
            [id] => 110
            [Cliente] => Alexis
            [body] => Hola
            [fecha] => 06/01/2021 11:35 AM
            [status] => 1
            [tiempoSinLeer] => 15 01:00
        )

    [1] => Array
        (
            [id] => 110
            [Cliente] => Alexis
            [body] => Hola asd asdasd
            [fecha] => 05/26/2021 11:35 AM
            [status] => 1
            [tiempoSinLeer] => 21 01:00
        )

    [2] => Array
        (
            [id] => 107
            [Cliente] => George
            [body] => vhgfshsgf
            [fecha] => 06/15/2021 03:48 PM
            [status] => 1
            [tiempoSinLeer] => 1 08:48
        )

)

So I'm trying to show the result in this way but I don't know how to do it, I tried some others methods like array_search, array_in and a foreach inside another foreach trying to see if inside can do an if statement but it doesn't work.

Array
(
    [0] => Array
        (
            [id] => 107
            [name] => Sara
            [msgSinleer] => 2
            [subject] => Array
               (
                    [Cliente] => George
                    [body] => vhgfshsgf
                    [fecha] => 06/15/2021 03:48 PM
                    [status] => 1
                    [tiempoSinLeer] => 1 08:48
               )
        )

    [1] => Array
        (
            [id] => 110
            [name] => Manuela 
            [msgSinleer] => 27
            [subject] => Array
               (
                    [Cliente] => Alexis
                    [body] => Hola
                    [fecha] => 06/01/2021 11:35 AM
                    [status] => 1
                    [tiempoSinLeer] => 15 01:00
               ),
               (
                    [Cliente] => Alexis
                    [body] => Hola asd asdasd
                    [fecha] => 05/26/2021 11:35 AM
                    [status] => 1
                    [tiempoSinLeer] => 21 01:00
               )
            
        )

)

And this is the code that I have that bring me the arrays

foreach ($query2 as $key2) {
   $result2[] = $key2;
}
        
foreach ($query as $value) {
      array_push($result , array(
       "id" => $value['id'],
       "name" => $value['name'],
       "msgSinLeer" => $value['msgSinleer'],
       "subject" => array(),
      ));
}

print_r ($result);
print_r ($result2);

I'm new in php so if someone can help me I will appreciate

  • Does this answer your question? [PHP: Merge 2 Multidimensional Arrays](https://stackoverflow.com/questions/1558291/php-merge-2-multidimensional-arrays) – Kinglish Jun 16 '21 at 18:22

1 Answers1

0

Following logic might help you on your way. Cycle through array $arr0 to find matching id's in $arr1. When a match is found, push to the 'subject' element in $arr0.

foreach($arr0 as $key => $record) {
    foreach($arr1 as $subject) {
        if($record['id'] === $subject['id']) $arr0[$key]['subject'][] = $subject;
    }
}

demo

jibsteroos
  • 1,366
  • 2
  • 7
  • 13