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 :

Concaténation de deux tableaux


Sujet :

Langage PHP

  1. #1
    Membre du Club
    Inscrit en
    Juin 2005
    Messages
    171
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 171
    Points : 68
    Points
    68
    Par défaut Concaténation de deux tableaux
    Bonjour,
    je reviens avec le probléme de concaténation de tableaux, aprés avoir réussi la concaténation des tableaux en un seul je me suis confronté a un probléme c'est la répétition des éléments .
    voici la fonction qui récuépre les pays et fait la concaténation:
    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
     
    function getCountriesList(){
    	$client = new VoxAPIClient('http://sandbox.voxbone.com/VoxAPI/services/VoxAPI?wsdl', true);
     
    	$token = getToken();
    	$request = new GetCountriesListRequest( $token );
    $request->setDIDType('GEOGRAPHIC');
    	$geographic = $client->getCountriesList( $request );
    	/*echo '<pre>';
     print_r($geographic);
     echo '</pre>';*/
     $token = getToken();
    	$request= new GetCountriesListRequest( $token );
            $request->setDIDType('NATIONAL');
     
    	$national= $client->getCountriesList( $request );
     
     
           $response= array_merge($geographic, $national);
     
     		//$response =array_unique($array);
     
           return $response;
    	/*echo '<pre>';
    		print_r($client->debug());
    	echo '</pre>';
    */
     
    	echo '<pre>';
    	print_r($response);
    	echo '</pre>';
     
    	return $response;
    }
    et le script qui fait l'affichage:
    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
     
    <form action="./index.php">
     
    	<?php
    		$countriesList = getCountriesList();
     
    			?>
    	<select name="CountryID" onChange="submit();">
     
    	<?php
    		if( !isset( $_GET['CountryID'] ) ){
    			echo "<option value=-1>------------------------------</option>";
    		}
    		foreach( $countriesList as $country ){
     
     
    			$opt = "<option value=$country->CountryID";
    			// default value is the selected value
    			if( isset( $_GET['CountryID'] ) && $_GET['CountryID'] == $country->CountryID ){
    				$opt = $opt." SELECTED>";
    			}else{
    				$opt = $opt.">";
    			}
    			$opt = $opt."$country->CountryName (+$country->CountryCode)</option>";
     
    			echo $opt;
    			}
     
     
    	?>
    	</select>
    	</form>
    Pour rappel les élément sont récupérer depuis un serveur via l'API SOAP

  2. #2
    Expert éminent
    Avatar de Séb.
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    5 199
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations professionnelles :
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Mars 2005
    Messages : 5 199
    Points : 8 424
    Points
    8 424
    Billets dans le blog
    17
    Par défaut
    aprés avoir réussi la concaténation des tableaux en un seul je me suis confronté a un probléme c'est la répétition des éléments .
    array_unique( ) permet de dédoubloner un tableau.
    http://fr.php.net/array-unique

  3. #3
    Membre du Club
    Inscrit en
    Juin 2005
    Messages
    171
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 171
    Points : 68
    Points
    68
    Par défaut
    au fait voici les changement que j'ai effectué au niveau de la fonction:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
     $array= array_merge($geographic, $national);
     
     		$response =array_unique($array);
     
           return $response;
    et voici le message d'erreur:
    Object of class stdClass could not be converted to string in...

  4. #4
    Expert éminent
    Avatar de Séb.
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    5 199
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations professionnelles :
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Mars 2005
    Messages : 5 199
    Points : 8 424
    Points
    8 424
    Billets dans le blog
    17
    Par défaut
    Montre comment tu l'utilises

  5. #5
    Membre du Club
    Inscrit en
    Juin 2005
    Messages
    171
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 171
    Points : 68
    Points
    68
    Par défaut
    voila les changements que j'ai fait au niveau de la fonction
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    $array= array_merge($geographic, $national);
     
     		$response =array_unique($array);
     
           return $response;
    et voici l'erreur:
    Object of class stdClass could not be converted to string in...

  6. #6
    Expert éminent
    Avatar de Séb.
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    5 199
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations professionnelles :
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Mars 2005
    Messages : 5 199
    Points : 8 424
    Points
    8 424
    Billets dans le blog
    17
    Par défaut
    Que donne ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    var_dump($geographic) ;
    echo '<hr>' ;
    var_dump($national) ;
    echo '<hr>' ;
    $array= array_merge($geographic, $national);
    echo '<hr>' ;
    var_dump($array) ;
    echo '<hr>' ;
     		$response =array_unique($array);
    echo '<hr>' ;
    var_dump($response) ;
     
           return $response;
    ?

  7. #7
    Membre du Club
    Inscrit en
    Juin 2005
    Messages
    171
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 171
    Points : 68
    Points
    68
    Par défaut
    voila ce que j'ai pour
    array(37) { [0]=> object(stdClass)#699 (5) { ["CountryID"]=> string(2) "10" ["CountryName"]=> string(9) "ARGENTINA" ["CodeA2"]=> string(2) "AR" ["CountryCode"]=> string(2) "54" ["HasStates"]=> string(5) "false" } [1]=> object(stdClass)#700 (5) { ["CountryID"]=> string(2) "13" ["CountryName"]=> string(9) "AUSTRALIA" ["CodeA2"]=> string(2) "AU" ["CountryCode"]=> string(2) "61" ["HasStates"]=> string(5) "false" } [2]=> object(stdClass)#26 (5) { ["CountryID"]=> string(2) "21" ["CountryName"]=> string(7) "BELGIUM" ["CodeA2"]=> string(2) "BE" ["CountryCode"]=> string(2) "32" ["HasStates"]=> string(5) "false" } [3]=> object(stdClass)#701 ....
    et pour national
    array(19) { [0]=> object(stdClass)#345 (5) { ["CountryID"]=> string(2) "14" ["CountryName"]=> string(7) "AUSTRIA" ["CodeA2"]=> string(2) "AT" ["CountryCode"]=> string(2) "43" ["HasStates"]=> string(5) "false" } [1]=> object(stdClass)#347 (5) { ["CountryID"]=> string(2) "17" ["CountryName"]=> string(7) "BAHRAIN" ["CodeA2"]=> string(2) "BH" ["CountryCode"]=> string(3) "973" ["HasStates"]=> string(5) "false" } [2]=> object(stdClass)#697 (5) { ["CountryID"]=> string(2) "21" ["CountryName"]=> string(7) "BELGIUM" ["CodeA2"]=> string(2) "BE" ["CountryCode"]=> string(2) "32" ["HasStates"]=> string(5) "false" } [3]=> object(stdClass)#346 (5) { ["CountryID"]=> string(2) "58" ["CountryName"]=> string(7) "DENMARK" ["CodeA2"]=> string(2) "DK" ["CountryCode"]=> string(2) "45" ["HasStates"]=> string(5) "false" } [4]=> object(stdClass)#342 (5) { ["CountryID"]=> string(2) "65" ["CountryName"]=> string(11) "EL SALVADOR" ["CodeA2"]=> string(2) "SV" ["CountryCode"]=> string(3) "503" ["HasStates"]=> string(5) "false" } [5]=> object(stdClass)#344 (5) { ["CountryID"]=> string(2) "68" ["CountryName"]=> string(7) "ESTONIA" ["CodeA2"]=> string(2) "EE" ["CountryCode"]=> string(3) "372" ["HasStates"]=> string(5) "false" } [6]=> object(stdClass)#343 (5) { ["CountryID"]=> string(2) "74" ["CountryName"]=> string(6) "FRANCE" ["CodeA2"]=> string(2) "FR" ["CountryCode"]=> string(2) "33" ["HasStates"]=> string(5) "false" } [7]=> object(stdClass)#339 (5) { ["CountryID"]=> string(3) "104" ["CountryName"]=> string(7) "IRELAND"
    et pour array:
    array(56) { [0]=> object(stdClass)#699 (5) { ["CountryID"]=> string(2) "10" ["CountryName"]=> string(9) "ARGENTINA" ["CodeA2"]=> string(2) "AR" ["CountryCode"]=> string(2) "54" ["HasStates"]=> string(5) "false" } [1]=> object(stdClass)#700 (5) { ["CountryID"]=> string(2) "13" ["CountryName"]=> string(9) "AUSTRALIA" ["CodeA2"]=> string(2) "AU" ["CountryCode"]=> string(2) "61" ["HasStates"]=> string(5) "false" } [2]=> object(stdClass)#26 (5) { ["CountryID"]=> string(2) "21" ["CountryName"]=> string(7) "BELGIUM" ["CodeA2"]=> string(2) "BE" ["CountryCode"]=> string(2) "32" ["HasStates"]=> string(5) "false" } [3]=> object(stdClass)#701 (5) { ["CountryID"]=> string(2) "30" ["CountryName"]=> string(6) "BRAZIL" ["CodeA2"]=> string(2) "BR" ["CountryCode"]=> string(2) "55" ["HasStates"]=> string(5) "false" } [4]=> object(stdClass)#702 (5) { ["CountryID"]=> string(2) "33" ["CountryName"]=> string(8) "BULGARIA" ["CodeA2"]=> string(2) "BG" ["CountryCode"]=> string(3) "359" ["HasStates"]=> string(5) "false" } [5]=> object(stdClass)#703 (5) { ["CountryID"]=> string(2) "38" ["CountryName"]=> string(6) "CANADA" ["CodeA2"]=> string(2) "CA" ["CountryCode"]=> string(1) "1" ["HasStates"]=> string(5) "false" } [6]=> object(stdClass)#704 (5) { ["CountryID"]=> string(2) "43" ["CountryName"]=> string(5) "CHILE" ["CodeA2"]=> string(2) "CL" ["CountryCode"]=> string(2) "56" ["HasStates"]=> string(5) "false" } [7]=> object(stdClass)#705 (5) { ["CountryID"]=> string(2) "54" ["CountryName"]=> string(7) "CROATIA" ["CodeA2"]=> string(2) "HR" ["CountryCode"]=> string(3) "385" ["HasStates"]=> string(5) "false" } [8]=> object(stdClass)#706 (5) { ["CountryID"]=> string(2) "56" ["CountryName"]=> string(6) "CYPRUS" ["CodeA2"]=> string(2) "CY" ["CountryCode"]=> string(3) "357" ["HasStates"]=> string(5) "false" } [9]=> object(stdClass)#707 (5) { ["CountryID"]=> string(2) "57" ["CountryName"]=> string(14) "CZECH REPUBLIC" ["CodeA2"]=> string(2) "CZ" ["CountryCode"]=> string(3) "420" ["HasStates"]=> string(5) "false" } [10]=> object(stdClass)#708 (5) { ["CountryID"]=> string(2) "61" ["CountryName"]=> string(18) "DOMINICAN REPUBLIC" ["CodeA2"]=> string(2) "DO" ["CountryCode"]=> string(1) "1" ["HasStates"]=> string(5) "false" } [11]=> object(stdClass)#709 (5) { ["CountryID"]=> string(2) "73" ["CountryName"]=> string(7) "FINLAND" ["CodeA2"]=> string(2) "FI" ["CountryCode"]=> string(3) "358" ["HasStates"]=> string(5) "false" } [12]=> object(stdClass)#710 (5) { ["CountryID"]=> string(2) "74" ["CountryName"]=> string(6) "FRANCE" ["CodeA2"]=> string(2) "FR" ["CountryCode"]=> string(2) "33" ["HasStates"]=> string(5) "false" } [13]=> object(stdClass)#711 (5) { ["CountryID"]=> string(2) "82" ["CountryName"]=> string(7) "GERMANY" ["CodeA2"]=> string(2) "DE" ["CountryCode"]=> string(2) "49" ["HasStates"]=> string(5) "false" } [14]=> object(stdClass)#712 (5) { ["CountryID"]=> string(2) "85" ["CountryName"]=> string(6) "GREECE" ["CodeA2"]=> string(2) "GR" ["CountryCode"]=> string(2) "30" ["HasStates"]=> string(5) "false" } [15]=> object(stdClass)#713 (5) { ["CountryID"]=> string(2) "98" ["CountryName"]=> string(7) "HUNGARY" ["CodeA2"]=> string(2) "HU" ["CountryCode"]=> string(2) "36" ["HasStates"]=> string(5) "false" }
    mais pour response y'a le message d'erreur d'avant:
    Catchable fatal error: Object of class stdClass could not be converted to string in...

  8. #8
    Expert éminent
    Avatar de Séb.
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    5 199
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations professionnelles :
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Mars 2005
    Messages : 5 199
    Points : 8 424
    Points
    8 424
    Billets dans le blog
    17
    Par défaut
    Bizarre ce msg car pour moi ça passe, exemple :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?php
        $a = array( ) ;
        $a[ ] = (object)array('id' => 123) ;
        $a[ ] = (object)array('id' => 234) ;
        $a[ ] = (object)array('id' => 123) ;
        $a[ ] = (object)array('id' => 123, 'foo' => 'bar') ;
     
        $a = array_unique($a) ;
     
        header('Content-Type: text/plain') ;
        print_r($a) ;
    ?>
    Donne bien :

    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
    Array
    (
        [0] => stdClass Object
            (
                [id] => 123
            )
     
        [1] => stdClass Object
            (
                [id] => 234
            )
     
        [3] => stdClass Object
            (
                [id] => 123
                [foo] => bar
            )
     
    )
    Quelle version de PHP utilises-tu ? Peux-tu tester le script plus haut sur ta config ?

    Sinon tu peux le faire à l'ancienne en bouclant sur ton tableau $array et en affectant chq élément à un nouveau tableau en te servant d'un identifiant unique (CountryID a priori) en tant que clef.

  9. #9
    Membre du Club
    Inscrit en
    Juin 2005
    Messages
    171
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 171
    Points : 68
    Points
    68
    Par défaut
    Excusez pour ce retard j'étais sur d'autre occupations en fait j'ai essayé le script que vous m'avez donné et ça affiche toujours la méme erreur:
    Catchable fatal error: Object of class stdClass could not be converted to string in
    et ma version de php est HP Version 5.2.8

  10. #10
    Expert éminent
    Avatar de Séb.
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    5 199
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations professionnelles :
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Mars 2005
    Messages : 5 199
    Points : 8 424
    Points
    8 424
    Billets dans le blog
    17
    Par défaut
    Ah bizarre
    Reste plus qu'à dédoublonner toi-même le tableau comme indiqué plus haut.

  11. #11
    Membre du Club
    Inscrit en
    Juin 2005
    Messages
    171
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 171
    Points : 68
    Points
    68
    Par défaut
    Pouvez vous me passer un bout de script pour cela j'ai vraiment des probléme avec les tableaux objet

  12. #12
    Expert éminent
    Avatar de Séb.
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    5 199
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations professionnelles :
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Mars 2005
    Messages : 5 199
    Points : 8 424
    Points
    8 424
    Billets dans le blog
    17
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    $distinct = array( ) ; // Tableau dédoublonné
    foreach ( array_merge($geographic, $national) as $country ) {
        $distinct[$country->CountryID] = $country ;
    }

  13. #13
    Membre du Club
    Inscrit en
    Juin 2005
    Messages
    171
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 171
    Points : 68
    Points
    68
    Par défaut
    Un grand merci seb ça marche parfaitement voici comment j'ai utilisé votre script dans la fonction:
    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
     
    function getCountriesList(){
    	$client = new VoxAPIClient('http://sandbox.voxbone.com/VoxAPI/services/VoxAPI?wsdl', true);
     
    	$token = getToken();
    	$request = new GetCountriesListRequest( $token );
    $request->setDIDType('GEOGRAPHIC');
    	$geographic = $client->getCountriesList( $request );
    	/*echo '<pre>';
     print_r($geographic);
     echo '</pre>';*/
     $token = getToken();
    	$request= new GetCountriesListRequest( $token );
            $request->setDIDType('NATIONAL');
     
    	$national= $client->getCountriesList( $request );
     
     
           //$response= array_merge($geographic, $national);
     
     		//$response =array_unique($array);
    				$response = array( ) ; // Tableau dédoublonné
    foreach ( array_merge($geographic, $national) as $country ) {
        $response[$country->CountryID] = $country ;
    }
           return $response;
    	/*echo '<pre>';
    		print_r($client->debug());
    	echo '</pre>';
    */
     
    	echo '<pre>';
    	print_r($response);
    	echo '</pre>';
     
    	return $response;
    }
    merci encore une fois de plus

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

Discussions similaires

  1. Concaténation de deux tableaux en C
    Par chercheur111 dans le forum C
    Réponses: 4
    Dernier message: 17/11/2013, 14h07
  2. PL/SQL COMPARAISON DE DEUX TABLEAUX APRES BULK
    Par mimi_été dans le forum PL/SQL
    Réponses: 5
    Dernier message: 30/06/2009, 12h14
  3. Concaténation de deux tableaux
    Par mego dans le forum Langage
    Réponses: 6
    Dernier message: 19/11/2008, 11h52
  4. Concaténer deux tableaux de cellules
    Par wawrint dans le forum MATLAB
    Réponses: 2
    Dernier message: 22/05/2008, 11h53
  5. Concaténation de deux integer pour former une clé primaire
    Par stoukou dans le forum Décisions SGBD
    Réponses: 2
    Dernier message: 08/09/2005, 10h34

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