IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

tableau (array) contient un autre array etc.


Sujet :

Langage PHP

  1. #1
    Membre habitué
    Profil pro
    Inscrit en
    Avril 2010
    Messages
    316
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Avril 2010
    Messages : 316
    Points : 155
    Points
    155
    Par défaut 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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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) :
    ]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

  2. #2
    Membre habitué
    Profil pro
    Inscrit en
    Avril 2010
    Messages
    316
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Avril 2010
    Messages : 316
    Points : 155
    Points
    155
    Par défaut array_push
    Bonjour,

    J'ai fait un essai avec "array_push" mais j'obtiens le même résultat.
    Voici mon code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    $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(PDO::FETCH_ASSOC))
    		{
     
     
    			$ville_population_mySql_array = array( $result['Name'] =>	array('title' => $result['Name'],	'population' => $result['Population']));
     
     
     
    			$ville_population_array = array( 'NY' =>	array('title' => 'NY',				'population' => $ny_population),
    											'OTW' =>	array('title' => 'OTW',				'population' => $otw_population));
     
    			 array_push($ville_population_array, $ville_population_mySql_array);
     
     
    			print_r($ville_population_array);
     
    		}
    voici ce que j'obtiens :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    Array
    (
        [NY] => Array
            (
                [title] => NY
                [population] => 8406000
            )
     
        [OTW] => Array
            (
                [title] => OTW
                [population] => 883391
            )
     
        [0] => Array
            (
                [Montréal] => Array
                    (
                        [title] => Montréal
                        [population] => 1016376
                    )
     
            )
     
    )
    Array
    (
        [NY] => Array
            (
                [title] => NY
                [population] => 8406000
            )
     
        [OTW] => Array
            (
                [title] => OTW
                [population] => 883391
            )
     
        [0] => Array
            (
                [Calgary] => Array
                    (
                        [title] => Calgary
                        [population] => 768082
                    )
     
            )
     
    )
    Array
    (
        [NY] => Array
            (
                [title] => NY
                [population] => 8406000
            )
     
        [OTW] => Array
            (
                [title] => OTW
                [population] => 883391
            )
     
        [0] => Array
            (
                [Toronto] => Array
                    (
                        [title] => Toronto
                        [population] => 688275
                    )
     
            )
     
    )
    Array
    (
        [NY] => Array
            (
                [title] => NY
                [population] => 8406000
            )
     
        [OTW] => Array
            (
                [title] => OTW
                [population] => 883391
            )
     
        [0] => Array
            (
                [North York] => Array
                    (
                        [title] => North York
                        [population] => 622632
                    )
     
            )
     
    )
    alors comment puis-je obtenir cela :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    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

  3. #3
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    Comme toujours, il faut faire simple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    while ($result = $req->fetch())
    			{
    				$ville_population_array[$result['Name']] = array( 'title' => $result['Name'],	'population' => $result['Population']);
    			}
    Au passage, tu as la même chose en clef dans tableau et dans "title" ; ca fait une information redondante.
    N'oubliez pas de consulter les FAQ PHP et les cours et tutoriels PHP

  4. #4
    Membre habitué
    Profil pro
    Inscrit en
    Avril 2010
    Messages
    316
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Avril 2010
    Messages : 316
    Points : 155
    Points
    155
    Par défaut
    Merci Sabotage,

    Ça marche comme je voulais.
    J'ai ajouté la fonction 'array_merge' :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    $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())
    			{
     
    				$ville_population_array[$result['Name']] = array( 'title' => $result['Name'],	'population' => $result['Population']);
     
    				$ville_population_form = array( 'NY' =>	array('title' => 'NY',				'population' => $ny_population),// new : 
    										 	'OTW' =>	array('title' => 'OTW',				'population' => $otw_population));// new : 
     
    				$ville_population_form = array_merge($ville_population_form,$ville_population_array);// new :
     
     
    			}
     
     
    		echo'<h1>ville population array MYSQL</h1>' ;
    		print_r($ville_population_array);
     
     
    		echo'<h1>ville population array MYSQL  + form</h1>' ;
    		print_r($ville_population_form);
     
     
    	}
    catch(Exception $e) 
    	{
    		throw $e;
    	}
    et j'obtins :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    Array
    	(
    		[NY] => Array
    		(
    			[title] => NY
    			[population] => 8406000
    		)
     
    		[OTW] => Array
    		(
    			[title] => OTW
    			[population] => 883391
    		)
     
    		[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
    		)
     
    	)
    Excelent!

    Citation Envoyé par sabotage Voir le message

    Au passage, tu as la même chose en clef dans tableau et dans "title" ; ca fait une information redondante.
    Oui tu as raison, mais je dois envoyer les informations au fournisseur sous cette forme bizarre...

    Merci encore

  5. #5
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    Non decidemment tu n'aimes pas quand c'est simple
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    $ville_population_form = array( 'NY' =>	array('title' => 'NY',				'population' => $ny_population),// new : 
    										 	'OTW' =>	array('title' => 'OTW',				'population' => $otw_population));
    while ($result = $req->fetch())
    			{
     
    				$ville_population_form[$result['Name']] = array( 'title' => $result['Name'],	'population' => $result['Population']);
     
    			}
    N'oubliez pas de consulter les FAQ PHP et les cours et tutoriels PHP

  6. #6
    Membre habitué
    Profil pro
    Inscrit en
    Avril 2010
    Messages
    316
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Avril 2010
    Messages : 316
    Points : 155
    Points
    155
    Par défaut
    Super cool Sabotage,

    Non je n'aime pas quand c'est compliqué, mais je ne suis pas encore capable de faire simple comme toi.
    J'espère qu'un jour, je peux avoir autant d’intellect subtil comme toi...

    Merci encore

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 14
    Dernier message: 20/08/2014, 14h12
  2. Réponses: 2
    Dernier message: 12/12/2013, 21h00
  3. Numpy : copier un array dans un autre array
    Par jlg_47 dans le forum Calcul scientifique
    Réponses: 3
    Dernier message: 24/03/2010, 10h23
  4. [Tableaux] copier tout un array dans une autre variable
    Par XavierWRC dans le forum Langage
    Réponses: 3
    Dernier message: 06/01/2010, 17h07
  5. Récupération d'un array dans un autre array
    Par slake13 dans le forum Langage
    Réponses: 1
    Dernier message: 14/10/2009, 16h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo