Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL][PHP] Ułożenie elementów tablicy, Odpowiednie posortowanie danych
max_mcee
post
Post #1





Grupa: Zarejestrowani
Postów: 156
Pomógł: 1
Dołączył: 25.09.2007

Ostrzeżenie: (0%)
-----


Cześć.
Mam problem z poukładaniem odpowiedni elementów.
Otrzymuje dane z bazy $data['c_list]:
  1. 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" } }


oraz
$data['a_list']:
  1. 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" } }


I teraz chciałbym otrzymać taki wynik:
Firma 1
--> adres główny
--> adres dodatkowy
Firma 2
--> adres główny

oto mój kod:

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


niestety to co "wyprodukowałem" dubluje dwukrotnie pierwszą firmę:

Wynik:
Firma 1
adres główny
adres dodatkowy
Firma 1
adres główny
adres dodatkowy
Firma 2
adres główny

Proszę o pomoc.
Dziękuję (IMG:style_emoticons/default/smile.gif)

Ten post edytował max_mcee 18.11.2023, 10:45:38
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 22.08.2025 - 21:02