Arranging the elements of the array, Properly sorting the data

I have a problem with arranging the correct items.

It receives data from the database $data[‘c_list]:

array(3) { [0]=> object(stdClass)#14 (3) { ["c_id"]=> int(1) ["company_name"]=> string(13) "Firma 1" ["name"]=> string(14) "adres główny" } [1]=> object(stdClass)#15 (3) { ["c_id"]=> int(1) ["company_name"]=> string(13) "Firma 1" ["name"]=> string(15) "adres dodatkowy" } [2]=> object(stdClass)#16 (3) { ["c_id"]=> int(3) ["company_name"]=> string(14) "Firma 2" ["name"]=> string(14) "adres główny" } } 

and
$data[‘a_list’]:

array(3) { [0]=> object(stdClass)#13 (3) { ["c_a_id"]=> int(1) ["c_id"]=> int(1) ["name"]=> string(14) "adres główny" } [1]=> object(stdClass)#18 (3) { ["c_a_id"]=> int(3) ["c_id"]=> int(1) ["name"]=> string(15) "adres dodatkowy" } [2]=> object(stdClass)#19 (3) { ["c_a_id"]=> int(4) ["c_id"]=> int(3) ["name"]=> string(14) "adres główny" } } 

And now I would like to get this result:
Firma 1
–> adres główny
–> adres dodatkowy
Firma 2
–> adres główny

here is my code:

    foreach ($data['c_list'] as $c => $key) {
        echo $key->company_name . '<br>';
        foreach ($data['a_list'] as $a)
        if($key->c_id == $a->c_id) {
             echo $a->name . '<br>';
         }
    }

Unfortunately, what I “produced” duplicates the first company twice:

Result:

Firma 1

adres główny

adres dodatkowy

Firma 1

adres główny

adres dodatkowy

Firma 2

adres główny

Please help me. Thank you.