How to combine two arrays based on keys?

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:

$posts_arr = array();
$posts_arr['lokacije'] = array();
$donacije = array();
while($row = $result->fetch(PDO::FETCH_ASSOC)){
    extract($row);
        $post_item = array(
            'id' => $id,
            'name' => $name,
            'state' => $state,
            'address' => $address,
            'phone' => $phone,
            'working_hours' => $working_hours,
            'description' => $description,
            'email' => $email,
            'fb_page' => $fb_page,
            'website' => $website,
            'additional_info' => $additional_info,
            'lat' => $lat,
            'lng' => $lng
        );
if(!in_array($id, array_column($posts_arr['lokacije'], 'id'))) {

    array_push($posts_arr['lokacije'], $post_item);
};
    
    if (empty($donacije[$id])) {
        $donacije[$id] = array();
    }
     $donacije[$id][] = $naziv;
     $posts_arr['lokacije'][$id][] = $naziv;
     
     
}

//print_r($donacije);
print_r($posts_arr);

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.

And stop using extract.

1 Like

Itā€™s not clear where each data comes from. Just use two indexed arrays, get a list of diff keys and use them for array_merge

        $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.

I would like to help but I am having a terrible time trying to import a picture of data into my database.

well if you really want to help then tech forums all over the world would love an OCR plugin, itā€™s desperately needed ^^

https://ocr.space/ did a decent job


****** 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 	

I uploaded my db, download it from the following link and import data.

https://filebin.net/x198op5slgt7tfdu

I didnā€™t find solution to this yet, can anybody help?

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.

1 Like

This solved my problem, thank you! :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service