tableau (array) contient un autre array etc.
Bonjour,
Voici la table MySql de ma basse de donnée :
ID...........Name..........................CountryCode.........District.............Population
1810..........Montréal...................CAN.................Québec............1016376
1811..........Calgary........................CAN................. Alberta............768082
1812..........Toronto........................CAN.................Ontario............688275
1813..........North York...................CAN.................Ontario............62263
...
18.............Arnhem.........................NLD.................Gelderland............138020
19.............Zaanstad.......................NLD.................Noord-Holland............135621
20.............´s-Hertogenbosch........NLD.................Noord-Brabant............129170
etc...
Ces données qui se trouve dans mySql que je vais mélanger avec d'autres données qui ne sont pas dans MySql.
Ce mélange sera dans une table [array] sous forme de :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| Array
(
[Montréal] => Array
(
[title] => Montréal
[population] => 1016376
)
[Calgary] => Array
(
[title] => Calgary
[population] => 768082
)
[Toronto] => Array
(
[title] => Toronto
[population] => 688275
)
[North York] => Array
(
[title] => North York
[population] => 622632
)
[NY] => Array
(
[title] => NY
[population] => 8406000
)
[OTW] => Array
(
[title] => OTW
[population] => 883391
)
) |
Comment puis-je obtenir ce tableau ci-dessus ?
Voici ce que je fais :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| $countryCode = 'CAN';
$ny_population = 8406000;
$otw_population = 883391;
$dbname1 = 'world';
$dsn1 = 'mysql:host=' . $host . ';dbname=' . $dbname1;
try
{
$bdd = new PDO($dsn1, $pdo_user1, $pdo_password1, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch (Exception $e)
{
die('Erreur : ' . $e->getMessage());
}
try
{
$req = $bdd->prepare
('SELECT Name,CountryCode,District,Population
FROM city
WHERE CountryCode = :CountryCode');
$req->execute
(array('CountryCode' => $countryCode));
while ($result = $req->fetch())
{
$mySqlWorldName = $result['Name'];
$mySqlWorldCountryCode = $result['CountryCode'];
$mySqlWorldDistrict = $result['District'];
$mySqlWorldPopulation = $result['Population'];
$ville_population_array = array( $mySqlWorldName => array('title' => $mySqlWorldName, 'population' => $mySqlWorldPopulation),
'NY' => array('title' => 'NY', 'population' => $ny_population),
'OTW' => array('title' => 'OTW', 'population' => $otw_population));
echo'<h1>population : ville (1ere T a n t a t i o n) :</h1>' ;
print_r($ville_population_array);
}
echo'<h1>population : ville (2e T a n t a t i o n) :</h1>' ;
print_r($ville_population_array);
}
catch(Exception $e)
{
throw $e;
} |
Avec la 1re tentation (Ligne 44 ), j'ai obtenu 4 tableaux différents :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| <h1>population : ville (1ere T a n t a t i o n) :</h1>Array
(
[Montréal] => Array
(
[title] => Montréal
[population] => 1016376
)
[NY] => Array
(
[title] => NY
[population] => 8406000
)
[OTW] => Array
(
[title] => OTW
[population] => 883391
)
)
<h1>population : ville (1ere T a n t a t i o n) :</h1>Array
(
[Calgary] => Array
(
[title] => Calgary
[population] => 768082
)
[NY] => Array
(
[title] => NY
[population] => 8406000
)
[OTW] => Array
(
[title] => OTW
[population] => 883391
)
)
<h1>population : ville (1ere T a n t a t i o n) :</h1>Array
(
[Toronto] => Array
(
[title] => Toronto
[population] => 688275
)
[NY] => Array
(
[title] => NY
[population] => 8406000
)
[OTW] => Array
(
[title] => OTW
[population] => 883391
)
)
<h1>population : ville (1ere T a n t a t i o n) :</h1>Array
(
[North York] => Array
(
[title] => North York
[population] => 622632
)
[NY] => Array
(
[title] => NY
[population] => 8406000
)
[OTW] => Array
(
[title] => OTW
[population] => 883391
)
) |
Avec la 2e tentation (Ligne 48 ), j'ai obtenu 1 seul tableau, mais seulement dernière ville qui vient de MySql :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <h1>population : ville (2e T a n t a t i o n) :</h1>Array
(
[North York] => Array
(
[title] => North York
[population] => 622632
)
[NY] => Array
(
[title] => NY
[population] => 8406000
)
[OTW] => Array
(
[title] => OTW
[population] => 883391
)
) |
Alors, est-ce que vous avez une idée pour avoir ce tableau ci-dessous (par contre, selon la requête sql (du pays) le contenu peut changer) :
Citation:
]Array
(
[Montréal] => Array
(
[title] => Montréal
[population] => 1016376
)
[Calgary] => Array
(
[title] => Calgary
[population] => 768082
)
[Toronto] => Array
(
[title] => Toronto
[population] => 688275
)
[North York] => Array
(
[title] => North York
[population] => 622632
)
[NY] => Array
(
[title] => NY
[population] => 8406000
)
[OTW] => Array
(
[title] => OTW
[population] => 883391
)
)
merci