I am trying to combine two arrays, as shown in picture there are two arrays one after another but IDs that are in circle are related and I would like to have bottom one nested into the first one, how can I achieve this. My code is here:
I would say that your “problem” is actually in your query. I would suggest you post an SQL dump of your DB along with a few sample records and a detailed description of what you want to know about the data.
$query = 'SELECT lokacija.*, donacija.naziv FROM lokacija, donacija, donacija_lokacija WHERE donacija.id=donacija_lokacija.donacija_id AND donacija_lokacija.lokacija_id=lokacija.id ORDER BY lokacija.name';
Query looks like above and data is as on picture, I have three tables, third table is just keeping IDs of first two tables and bridging them. Most of the rows are repeated exactly same, the only different column is the last one ‘naziv’. And i want to output distinct rows while collecting different ‘naziv’ values for the same ID and putting them in the array.
****** Result for Image/Page 1 ******
+ Options
name 1 state address phone working_hours description email fb_page website additional Info lat Ing nazvv
JLI Ojeöji dom Crna +382/31/671-042 JIJ Ojeöji dom "Mladost" u 8ijeloj je Odeéa za
2 Bijela bb, Herceg Novi [email protected] http:/hwnv.dombijela.me/ 42.454727 18.660122
"Mladost' Gora +382/31/671-033 ustanava saci... decu
Sredstva
Svetazara Markoviéa 42a, Svakim radnim danom ad LICEULICE je neprofitna arganizacjja
34 Liceulice Srbija +381122686567 https:ffmm.facebaak.cam!LlCEULlCE/ https:/fwwv.'.liceulice.arg.f 44.8032981 20.4632948 za liönu
Beograd 10-1711 posveiena pod.
higijenu
Svetazara Markoviéa 42a, Svakim radnim danom ad LICEULICE je neprofitna arganizacija
34 Liceulice Srbija +381122686567 [email protected] https://www.facebaak.cam/LlCEULlCE/ https:f/www.liceulice.arg/ 44.8032981 20.4632948 Hrana
Beograd 10-1711 posveiena pod.
Svetazara Markoviéa 42a: Svakim radnim danom ad LICEULICE je neprofitna arganizacija Obuéa za
34 Liceulice Srbija +381122686567 https:f/wvuefacebaak.camiLlCEULlCE/ https:/fwwv.'.liceulice.arg/ 44.8032981 20.4632948
Beograd 10-1711 posveiena pod. odrasle
Svetazara Markoviéa 42a, Svakim radnim danom ad LICEULICE je neprofitna arganizacija
34 Liceulice Srbija +381122686567 [email protected] https://www.facebaak.cam/LlCEULlCE/ https://www.liceulice.arg/ 44.8032981 20.4632948 Posteljina
Beograd 10-1711 posveiena pod.
Svetazara Markoviéa 42a, Svakim radnim danom ad LICEULICE je neprofitna arganizacjja Odeéa za
34 Liceulice https://mm.facebaak.camiLlCEULlCE/ https:f/mm.liceulice.arg/ 44.8032981 20.4632948
Beograd 10-1711 posveéena pod. odrasla
Rad Cross af Odeéa za
Serbia Barajevska 1, Sarajevo 011/830-2177 Nacianalno drustvo [email protected] https://www.facebaak.camjcrvenikrst-beograd https://www.crvenikrst011.org.rs - 44.8054516 20.4573684
Serbia decu
14paeHH Cpülje je
Rad Cross af Simina 19. Beograd 11000,
13 Serbia +381 11 3032125 Nacianalno drustvo https:/fwwv.'.redcras:s.arg.rs yaUHOHaj7HO gpyurao 44.8190369 20.4607885 Hrana
Serbia Serbia
caCTOJH...
14paeHM KPCT Cpähje je
Red Cross af Simina 19, Beograd 11000, Obuéa za
13 Serbia +381 11 3032125 Nacianalno drustvo [email protected] https://www.facebaak.cam/redcrassofserbia https:f/www.redcrass.arg.rs yauHOHanyo gpyurso h 44.8190369 20.4607885
Serbia Serbia odrasle
caCTOJH...
Red Cross af Ustaniika 84 / 13 sprat, Odeéa za
21 Serbia 011/2456-616, 2457-715 Nacianalno drustvo [email protected] https://wwv.'.facebaak.camicrvenikrst-becgrad https:ffmm.crvenikrst011 .org.rs - 44.7858625 20.4806517
Serbia Beograd decu
Rad Cross af Odeéa za
16 Serbia Studentski trg 18 01•1/2628-882 Nacianalno drustvo [email protected] https://www.facebaak.camjcrvenikrst-beograd vmw.crvenikrststarigrad.org.rs 44.8197021 20.4585515
Serbia decu
14paeHH Cpülje je
Rad Cross af Simina 19. Beograd 11000, Odeéa za
13 Serbia +381 11 3032125 Nacianalno drustvo https:/fwwv.'.redcras:s.arg.rs yaUHOHaj7HO gpyurao 44.8190369 20.4607885
Serbia Serbia cdrasle
caCTOJH...
Rad Cross af Bulevar oslobodenja Odeéa za
Serbia 01•1/8500-940 Nacianalno drustvo [email protected] https://www.facebaak.camjcrvenikrst-beograd https://www.crvenikrst011.org.rs - 44.6713339 20.7193017
Serbia 39, Grocka decu
Red Cross af Odeéa za
23 Serbia Miöka Kranjca 2a. Beograd 011/3565-108 Nacianalno drustvo https://wwn.facebaak.cam/crvenikrstbecgrad https:/fwwvecrvenikrst011 .org.rs - 44.7428049 20.4424472
Serbia decu
Red Cross af Odeéa za
14 serbia Savska 2, 11 080 Zemun 011/3161-880 Nacianalno drustvo [email protected] https:f/www.facebaak.camjcrvenikrst-beograd https://www.crvenikrst011.org.rs - 44.8071867 20.4555299
Serbia odrasle
14paeHH Cpülje je
Red Cross af Simina 19. Beograd 11000,
13 Serbia +381 11 3032125 Nacianalno drustvo https://vww.'.facebaak.camiredcrassofserbia https://wwv.'.redcras:s.arg.rs H a,L4MOHaj7HO gpyurao 44.8190369 20.4607885 Knjige
Serbia Serbia
caCTOJH...
Red Cross af Odeéa za
Serbia Cvijiéeva 75 011/2769-150 Nacianalno drustvo [email protected] https:f/www.facebaak.camjcrvenikrst-beograd vmw.crvenikrst011.org.rs 44.8113186 20.4792071
Serbia decu
14paeHH Cpülje je
Red Cross af Simina 19. Beograd 11000,
13 Serbia +381 11 3032125 Nacianalno drustvo https://vww.'.facebaak.camiredcrassofserbia https://wwv.'.redcras:s.arg.rs H a,L4MOHaj7HO gpyurao 44.8190369 20.4607885 Posteljina
Serbia Serbia
caCTOJH...
Red Cross af Odeéa za
Serhii; Kn.e•a Milne 41 R nnnt ennntnrpdrrnee re far•ehnnk rnm/r•nænikret 44 •0 5744061
What end result (overall goal) do you want? Do you actually want a combined array for some reason (and what would that reason be?) or are you just trying to display this information, with each distinct location followed by a list of the donations for the location?
I think I figured out what you are trying to accomplish (the picture in the 1st post is confusing and couldn’t have come from the query you posted later.)
See if this produces a result that does what you want -
$posts_arr = array();
while($row = $result->fetch(PDO::FETCH_ASSOC))
{
if(!isset($posts_arr[$row['id']]))
{
// create an entry for each distinct location
$posts_arr[$row['id']] = $row; // note: this will contain the 1st 'naziv' value per location - not used.
}
// store the naziv values as a sub array in the location entry
$posts_arr[$row['id']]['donations'][] = $row['naziv'];
}
print_r($posts_arr);
Note: if you set the default fetch mode to assoc when you make the database connection, you won’t have to specify it in each fetch statement.