A
A
Arayik Mikayelyan2015-05-14 10:47:53
PHP
Arayik Mikayelyan, 2015-05-14 10:47:53

How to remove elements from an array by groups?

There is a table of cities, they indicate countries in the form of id of these countries from the countries table. How can I display cities by country from an array. It should be like this:
Russia: Moscow, St. Petersburg....
Belarus: Minsk, Mogilev....
Kazakhstan: Astana, Temirtau....
It is desirable that the output be automatically recognized by the country id. In order not to manually check each country, in case new countries are added.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
isratmir, 2015-05-14
@isratmir

Make a selection from the database:

SELECT 
  country.name, cities.name
FROM 
  country 
LEFT JOIN 
  cities
ON 
  country.id=cities.country_id

H
Hakhagmon, 2015-05-14
@Hakhagmon

$query = "SELECT * from contry";
$res = mysql_query( $query );

     while ( $item = mysql_fetch_array( $res ) )

    { 
if ($item['country.name']=="Россия") {$rus.= $item['city.name'].' '; };
if ($item['country.name']=="Беларусь") {$bel.= $item['city.name'].' '; };       		
if ($item['country.name']=="Казахстан") {$kz.= $item['city.name'].' '; };
}

echo 'Россия: '.$rus;
echo 'Беларусь: '.$bel;
echo 'Казахстан :'.$kz;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question